Python 根据地址获取经纬度
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python 根据地址获取经纬度相关的知识,希望对你有一定的参考价值。
方法一:
使用Geopy包 : https://github.com/geopy/geopy (仅能精确到城镇,具体街道无结果返回)
from geopy.geocoders import Nominatim #使用geopy查询 def geocodeN(address): gps=Nominatim() location=gps.geocode(address) return location.longitude,location.latitude
方法二:
使用高德地图或百度地图API,key可去官网申请替换
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‘]
计算两个经纬度间距离:
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=2*asin(sqrt(a))*6371*1000 return dis
以上是关于Python 根据地址获取经纬度的主要内容,如果未能解决你的问题,请参考以下文章