python根据地址获取经纬度并计算两个经纬度间距离。代码如下:
python根据地址获取经纬度方法一:
- from geopy.geocoders import Nominatim
#使用geopy查询
def geocodeN(address):
gps=Nominatim()
location=gps.geocode(address)
return location.longitude,location.latitude
使用Geopy包 : https://github.com/geopy/geopy (仅能精确到城镇,具体街道无结果返回)
python根据地址获取经纬度方法二:
- import requests
#使用高德API
def geocodeG(address):
par = {'address': address, 'key': 'cb649a25c1f81c1451adbeca73623251'}
base = 'http://restapi.amap.com/v3/geocode/geo'
response = requests.get(base, par)
answer = response.json()
GPS=answer['geocodes'][0]['location'].split(",")
return GPS[0],GPS[1]
#使用百度API
def geocodeB(address):
base = url = "http://api.map.baidu.com/geocoder?address=" + address + "&output=json&key=f247cdb592eb43ebac6ccd27f796e2d2"
response = requests.get(base)
answer = response.json()
return answer['result']['location']['lng'],answer['result']['location']['lat']
提示:使用高德地图或百度地图API,key可去官网申请替换
计算两个经纬度间距离:
- from math import radians, cos, sin, asin, sqrt
#计算两点间距离-m
def geodistance(lng1,lat1,lng2,lat2):
lng1, lat1, lng2, lat2 = map(radians, [lng1, lat1, lng2, lat2])
dlon=lng2-lng1
dlat=lat2-lat1
a=sin(dlat/2)2 + cos(lat1) cos(lat2) sin(dlon/2)2
dis=2asin(sqrt(a))6371*1000
return dis
以上就是“python根据地址获取经纬度并计算两个经纬度间距离”的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流。