geopy 獲取經緯度計算距離

2022-07-09 15:09:12 字數 3083 閱讀 6982

from geopy.geocoders import nominatim

from geopy.distance import geodesic,great_circle

from urllib import parse

import hashlib, json, requests

def get_urt(address):

querystr = '/geocoding/v3/?address=%s&output=json&ak=**yourak**' % address

encodestr = parse.quote(querystr, safe="/:=&?#+!$,;'@()*")

rawstr = encodestr + '**yoursk**'

sn = hashlib.md5(parse.quote_plus(rawstr).encode("utf-8")).hexdigest()

url = parse.quote(''+querystr+'&sn='+sn, safe="/:=&?#+!$,;'@()*")

# print(url)

return url

'''獲取經緯度'''

location_url = get_urt('蘇州觀前街')

res_location = requests.get(location_url).text

location_js = json.loads(res_location)

lat = location_js['result']['location']['lat']

lon = location_js['result']['location']['lng']

print(lat, lon)

def get_route_matrix(type,origin, destination):

'''type: walking,riding,motorcycle,driving

tractics:駕車、電單車可設定,10:不走高速,11:常規路線,12:距離較短(計算耗時考慮路況),13:距離較短(計算耗時不考慮路況)

riding_type:電動車、自行車可設定。0:普通自行車,1:電動自行車

'''querystr = '/routematrix/v2/%s?output=json&origins=%s&destinations=%s&ak=grax38f9qxhsovgubsu6aen1ueo9mcm8' % \

(type, origin, destination)

url = parse.quote(''+querystr,safe="/:=&?#+!$,;'@()*")

return url

'''批量算路,獲取到達時間'''

route_url = get_route_matrix('driving','31.318035,120.622065|31.31094,120.582934','32.551671,120.220522|32.531951,120.248576')

res_route = requests.get(route_url).text

print(res_route)

route_js = json.loads(res_route)

route_res = route_js['result']

for i in range(len(route_res)):

print('od'+str(i),route_res[i]['distance']['text'],route_res[i]['duration']['text'])

geolocator = nominatim(user_agent='geo_test')

location = geolocator.geocode('白公尺鎮,泰州市')

print(location.address)

print(location.latitude, location.longitude)

print(location.raw)

latlon =

location1 = geolocator.reverse("%(latitude)s, %(longitude)s"%latlon)

print(location1.address)

print(location1.latitude, location1.longitude)

print(location1.raw)

winson_loc = (41.97772,122.580011)

ivan_loc = (-37.775789,144.941926)

dis_geodesic = geodesic(winson_loc,ivan_loc).miles

print('geodesic: '+str(dis_geodesic))

dis_gcircle = great_circle(winson_loc,ivan_loc).miles

print('great_circle: '+str(dis_gcircle))

iOS獲取經緯度

在ios8.0以上獲取經緯度時,需要申請授權,否則不能定位 第一步 在 homeviewcontroller.m inte ce homeviewcontroller cllocationmanagerdelegate 第二步 設定定位 void setuplocationmanager locat...

window phone 獲取經緯度資訊

ps 因為需要冷啟動至少也需要一分鐘以上,所以最好在公共類初始化,在需要呼叫的時候直接使用。geocoordinatewatcher 類提供基於座標的位置資料,其來自當前的位置提供程式。來自當前位置提供程式的基於座標的位置資料,該提供程式是計算機上優先級別最高的位置提供程式,其優先級別取決於一系列因...

Android中如何獲取經緯度

前提是 在這之前jingwd 方法被呼叫過了,比如放在oncreate 裡,因為這裡面 是具體的實現,下面是 private locationmanager locationmanager private string locationprovider 經緯度獲取 private void jing...