from geopy.geocoders import Nominatim
# # 1.需有时候会出错或者超时,没有国家信息 可以计算两点之间的距离
# geolocator = Nominatim() #实例化对象
# location = geolocator.geocode('地址') #获取位置
# print(location.address)
# def geocodeN(address):
# gps=Nominatim()
# location=gps.geocode(address)
# return location.longitude,location.latitude
#
# # print(geocodeN("地址"))
# print(geocodeN("地址"))
# ------------
# 2.这个全不全不确定,可以看到bbox,有国家省市信息,但是用处不大 会限速,有时候会出错 还有百度中文输出没找到资料
# https://www.jianshu.com/p/59a7431a552d
# import geocoder
#
# g = geocoder.google("1403 Washington Ave, New Orleans, LA 70130")
#
# # g = geocoder.arcgis(u"地址")
# g = geocoder.arcgis(u"地址")
#
# print(g.latlng)
# print(g.geojson)
# # # print(g.bbox)
#-------------
# 3.高德地图 带国家名字和省市区。首选
# 高德地图 请求成功 状态码count 失败0 成功1 和百度相反
import requests
# def geocode(address):
# parameters = {'address': address, 'key': 此处添加自己在高德地图上申请的API_KEY'}
# url = 'https://restapi.amap.com/v3/geocode/geo?parameters'
# response = requests.get(url, parameters)
# # answer = response.geocodes['location']
# # print(answer)
# answer = response.json()
# if answer['count'] == str(1):
# print(address + "的经纬度:", answer['geocodes'][0]['location'])
# location = answer['geocodes'][0]['location'].split(',')
# country = answer['geocodes'][0]['country']
# province = answer['geocodes'][0]['province']
# city = answer['geocodes'][0]['city']
#
# lat = location[1] #纬度
# lng = location[0] #经度
#
# print(lat)
# print(lng)
# print(country)
# print(province)
# print(city)
#
#
# if __name__ == '__main__':
# geocode('地址')
# # geocode('地址')
# 4.百度地图api,不带国家具体信息
# 百度地图:请求状态吗 status 成功0 失败 1 和高德相反
# http://www.voidcn.com/article/p-apeiortj-bpp.html
import json
from urllib.request import urlopen, quote
import csv
import requests
def getlnglat(address):
url = 'http://api.map.baidu.com/geocoder/v2/'
output = 'json'
ak = '此处添加自己在百度地图上申请的API_KEY'
add = quote(address) #由于本文地址变量为中文,为防止乱码,先用quote进行编码
uri = url + '?' + 'address=' + add + '&output=' + output + '&ak=' + ak
req = urlopen(uri)
res = req.read().decode()
temp = json.loads(res)
# print(temp.keys())
# print(temp.values())
# print(type(temp['result']))
if temp['status'] == 0 :
lng = temp['result']['location']['lng'] # 纬度
lat = (temp['result']['location']['lat']) # 经度
print(lat, lng)
else:
print(address+"没找到")
with open('D:\pycharm\\test_python_csv\\island\\test111.csv', 'w+', newline='') as csvfile:
writer = csv.writer(csvfile)
try:
if temp['status'] == 0:
lng = temp['result']['location']['lng'] # 纬度
lat = (temp['result']['location']['lat']) # 经度
writer.writerow([address, lat, lng])
print(00)
# else:
# writer.writerow([address])
# print(11)
except:
print(22)
writer.writerow([address])
getlnglat('地址')
# ###getlnglat('地址')
# KEY = xxx' # 此处添加自己在Google地图上申请的API_KEY
# import requests
# url = 'https://maps.googleapis.com/maps/api/geocode/json?address=珍珠岛此处添加自己在Google地图上申请的API_KEY'
# req = requests.get(url)
# print(req.json())
# ### {'results': [], 'status': 'ZERO_RESULTS'}
版权声明:本文为weixin_41561539原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。