用Python获取香港地理编码的问题

2024-04-25 20:31:45 发布

您现在位置:Python中文网/ 问答频道 /正文

在python(Jupyter笔记本)中,“getGeoCode”具有“getGeoBoundry(location_name)”函数。对于这个问题,您需要API访问

它返回json格式,如下所示: {u'northeast':{u'lat':22.5619469,u'lng':114.4294999},u'southwest':{u'lat':22.1435,u'lng': 113.8259001 }}带有LoopyOnNe= =“香港”

如需帮助:-https://developers.google.com/maps/documentation/geocoding/intro

我不能用python编写这个函数吗


Tags: 函数nameapijson格式笔记本jupyterlocation
1条回答
网友
1楼 · 发布于 2024-04-25 20:31:45

要对“Hong Kong”进行地理编码,只需将以下代码(基于related thread中的代码)添加到Google colab笔记本中的单元格中:

import httplib2
import json

def getGeocodeLocation(inputString):
    # Use Google Maps to convert a location into Latitute/Longitute coordinates

    google_api_key = "YOUR_API_KEY"
    locationString = inputString.replace(" ", "+")
    url = ('https://maps.googleapis.com/maps/api/geocode/json?address=%s&key=%s'% (locationString, google_api_key))
    h = httplib2.Http()
    result = json.loads(h.request(url,'GET')[1])
    latitude = result['results'][0]['geometry']['location']['lat']
    longitude = result['results'][0]['geometry']['location']['lng']
    return result;

getGeocodeLocation("Hong Kong")

然后单击“运行单元格”,您将获得以下输出:

{'results': [{'address_components': [{'long_name': 'Hong Kong',
     'short_name': 'HK',
     'types': ['country', 'political']}],
   'formatted_address': 'Hong Kong',
   'geometry': {'bounds': {'northeast': {'lat': 22.5619469,
      'lng': 114.4294999},
     'southwest': {'lat': 22.1435, 'lng': 113.8259001}},
    'location': {'lat': 22.3193039, 'lng': 114.1693611},
    'location_type': 'APPROXIMATE',
    'viewport': {'northeast': {'lat': 22.5619469, 'lng': 114.4294999},
     'southwest': {'lat': 22.1435, 'lng': 113.8259001}}},
   'place_id': 'ChIJD5gyo-3iAzQRfMnq27qzivA',
   'types': ['country', 'political']}],
 'status': 'OK'}

希望这有帮助

相关问题 更多 >