zoukankan      html  css  js  c++  java
  • 谷歌、腾讯、百度相应API批量获取地理位置坐标信息及其优缺点

    目录:

    • 申请ak
    • 批量获取地理位置
    • 优缺点对比

    目的:通过给定的地理位置名称(如:北京市海淀区上地十街十号),获取经纬度信息。

    1、申请ak

    以百度Geocoding API为例: http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding

    • 提交之后就会有访问应用的AK,这就是你访问的ak:

    2、批量获取地理坐标

    对于以下文本文件里的城市:

    这里是对以上文本文件里的城市进行查询地理位置的代码:

     1 # coding : utf-8
     2 
     3 """
     4 This program use api to get lon/lat data.
     5 """
     6 
     7 import sys
     8 import requests
     9 
    10 def get_degree():
    11 
    12     local_path = 'D:/home/research/lung_cancer/data/pm25/process/'
    13 
    14     # 把文本文件里的城市全部放入list里面
    15     csv_path = local_path + 'city_2014.csv'
    16     loc_list= []
    17     line_list = []
    18     with open(csv_path, 'r') as handle:
    19         for line in handle.readlines()[1:]:
    20             loc = line.split(',')[1]
    21             loc_list.append(loc)
    22             line_list.append(line.strip())
    23 
    24     num = 0
    25 
    26     # 将坐标信息写入的新文本文件
    27     new_path = local_path + 'city_2014_google.csv'
    28     f_handle = open(new_path, 'w')
    29 
    30     for i in range(len(loc_list)):
    31         loc = loc_list[i]
    32         try:
    33             # Three type apis:baidu,qq,google.
    34             # 百度API
    35             # request = ('http://api.map.baidu.com/geocoder/v2/?address=' + loc 
    36             #         + '&output=json&ak=你的ak')
    37             # 腾讯API
    38             # request = ('http://apis.map.qq.com/ws/geocoder/v1/?address=' + loc
    39             #         + '&key=你的key')
    40             # 谷歌API
    41             request = ('https://maps.googleapis.com/maps/api/geocode/json?address='
    42                     + loc + '&key=你的key')
    43             response = requests.get(request)
    44             response.raise_for_status()
    45             num += 1
    46             print('HTTP request successed!--{}'.format(num))
    47         except Exception as e:
    48             print('HTTP request failed!-{}'.format(str(e)))
    49 
    50         print(response.text)
    51 
    52         # eval将json字符串转变为字典
    53         lat = eval(response.text)['results'][0]['geometry']['location']['lat']
    54         lng = eval(response.text)['results'][0]['geometry']['location']['lng']
    55         # 将坐标信息写入硬盘
    56         f_handle.write(line_list[i] + ',' + str(round(lat,2)) + ',' 
    57                        + str(round(lng,2)) + '
    ') 

    查询时会返回这样的信息,可以是json或者xml,这里我选择的是json: 

    这里是结果,第3、4个分别是纬度和经度:

    3、优缺点对比

    限制指的是限速和限量。

    百度api:限制小,但是不准确; http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding
    腾讯api:虽准确,但限制太大;
    谷歌api:限制中等,且准确;(最终用) https://developers.google.com/maps/documentation/javascript/geocoding

  • 相关阅读:
    slf4j使用
    centos 安装 redis
    centos安装单机zookeeper
    activemq消息重发机制[转]
    maven 指定工程的 jdk 版本及编译级别
    Elasticsearch-索引新数据(创建索引、添加数据)
    Elasticsearch-数据的存储、搜索(干货)
    Yarn-本地获取任务日志
    Hive-Container killed by YARN for exceeding memory limits. 9.2 GB of 9 GB physical memory used. Consider boosting spark.yarn.executor.memoryOverhead.
    Elasticsearch-安装、日志解读
  • 原文地址:https://www.cnblogs.com/echo-coding/p/8667829.html
Copyright © 2011-2022 走看看