【问题标题】:getting distance between two location using geocoding使用地理编码获取两个位置之间的距离
【发布时间】:2016-04-29 22:48:02
【问题描述】:

我想使用 google API 查找两个位置之间的距离。我希望输出看起来像 - “位置 1 和位置 2 之间的距离是 500 英里(此处的距离是示例目的)”,但是由于当前程序显示各种输出(我无法使用),我如何获得所需的输出得到他想要的输出)。你们能告诉我方法吗?或者告诉我具体的程序是什么?

   import urllib
   import json
   serviceurl = 'http://maps.googleapis.com/maps/api/geocode/json?'

   while True:
   address = raw_input('Enter location: ')
   if len(address) < 1 : break

   url = serviceurl + urllib.urlencode({'sensor':'false', 'address': address})
   print 'Retrieving', url
   uh = urllib.urlopen(url)
   data = uh.read()
   print 'Retrieved',len(data),'characters'

   try: js = json.loads(str(data))
   except: js = None
   if 'status' not in js or js['status'] != 'OK':
    print '==== Failure To Retrieve ===='
    print data
    continue

   print json.dumps(js, indent=4)

   lat = js["results"][0]["geometry"]["location"]["lat"]
   lng = js["results"][0]["geometry"]["location"]["lng"]
   print 'lat',lat,'lng',lng
   location = js['results'][0]['formatted_address']
   print location

【问题讨论】:

    标签: python python-2.7 google-api google-geocoding-api


    【解决方案1】:

    Google 有一个专门的 api,称为 Google Maps Distance Matrix API

    多个目的地和交通方式的距离和持续时间
    根据推荐路线检索持续时间和距离值 在起点和终点之间。


    如果您只需要地球上两点之间的距离,您可能需要使用Haversine formula

    【讨论】:

      【解决方案2】:

      如果您知道 latlon,请使用 geopy 包:

      In [1]: from geopy.distance import great_circle
      
      In [2]: newport_ri = (41.49008, -71.312796)
      
      In [3]: cleveland_oh = (41.499498, -81.695391)
      
      In [4]: great_circle(newport_ri, cleveland_oh).kilometers
      Out[4]: 864.4567616296598
      

      【讨论】:

        猜你喜欢
        • 2020-10-06
        • 2011-12-24
        • 2011-02-14
        • 2012-12-12
        • 2013-08-21
        • 1970-01-01
        • 2010-11-09
        相关资源
        最近更新 更多