Python GoogleMaps API - 地址到坐标(纬度,经度)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15285691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:41:58  来源:igfitidea点击:

GoogleMaps API -address to coordinates (latitude,longitude)

pythongoogle-mapsgoogle-geocoding-api

提问by MiniMe

This is driving me crazy. I have deleted this key 1000 times so far. Yesterday it worked like a charm, today not anymore Here is the python code:

这真让我抓狂。到目前为止,我已经删除了这个键 1000 次。昨天它就像一个魅力,今天不再是这里的python代码:

from googlemaps import GoogleMaps
gmaps = GoogleMaps("AIzaSyBIdSyB_td3PE-ur-ISjwFUtBf2O0Uo0Jo")
exactaddress ="1 Toronto Street Toronto"
lat, lng = gmaps.address_to_latlng(exactaddress)
print lat, lng

GoogleMapsError: Error 610: G_GEO_BAD_KEY

It is now returning the above error for no obvious reasons. I don't think I have reached the request limit or the maximum rate To stay on the safe side I even introduced delays (1sec) ...stil getting the same error

它现在没有明显的原因返回上述错误。我认为我没有达到请求限制或最大速率为了安全起见,我什至引入了延迟(1 秒)...仍然遇到相同的错误

Does anybody have any idea how I can solve this? Having to work with a different python module is fine if you can indicate an alternative to the one that I am currently using.

有谁知道我该如何解决这个问题?如果您可以指出我当前使用的模块的替代方案,则必须使用不同的 python 模块是可以的。

thanks C

谢谢 C

PS: the key is valid, it is a client key and it was automatically enabled when I enabled GoogleMAP API3 in the App console. No restrictions for domains or IPs

PS:密钥有效,它是一个客户端密钥,当我在应用程序控制台中启用 GoogleMAP API3 时它会自动启用。对域或 IP 没有限制

EDIT: So here is what I ended up using

编辑:所以这是我最终使用的

def decodeAddressToCoordinates( address ):
        urlParams = {
                'address': address,
                'sensor': 'false',
        }  
        url = 'http://maps.google.com/maps/api/geocode/json?' + urllib.urlencode( urlParams )
        response = urllib2.urlopen( url )
        responseBody = response.read()

        body = StringIO.StringIO( responseBody )
        result = json.load( body )
        if 'status' not in result or result['status'] != 'OK':
                return None
        else:
                return {
                        'lat': result['results'][0]['geometry']['location']['lat'],
                        'lng': result['results'][0]['geometry']['location']['lng']
                }  

The library that Jason pointed me to is also interesting but since my code was intended to fix something (one time use) I have not tried his solution. I will definitely consider that if I get to write code again :-)

Jason 指向我的库也很有趣,但由于我的代码旨在修复某些内容(一次使用),因此我没有尝试过他的解决方案。如果我再次编写代码,我肯定会考虑:-)

采纳答案by jasonsee

Although Google deprecated the V2 calls with googlemaps (which is why you're seeing the broken calls), they just recently announced that they are giving developers a six-month extension (until September 8, 2013) to move from the V2 to V3 API. See Update on Geocoding API V2for details.

尽管 Google 弃用了带有 googlemaps 的 V2 调用(这就是您看到中断调用的原因),但他们最近才宣布他们将向开发人员提供六个月的扩展期(直到 2013 年 9 月 8 日),以从 V2 迁移到 V3 API . 有关详细信息,请参阅地理编码 API V2 更新

In the meantime, check out pygeocoderas a possible Python V3 solution.

同时,查看pygeocoder作为可能的 Python V3 解决方案。

回答by Dennis Golomazov

Since September 2013, Google Maps API v2 no longer works. Here is the code working for API v3 (based on this answer):

自 2013 年 9 月起,Google Maps API v2不再有效。这是适用于 API v3 的代码(基于此答案):

import urllib
import simplejson

googleGeocodeUrl = 'http://maps.googleapis.com/maps/api/geocode/json?'

def get_coordinates(query, from_sensor=False):
    query = query.encode('utf-8')
    params = {
        'address': query,
        'sensor': "true" if from_sensor else "false"
    }
    url = googleGeocodeUrl + urllib.urlencode(params)
    json_response = urllib.urlopen(url)
    response = simplejson.loads(json_response.read())
    if response['results']:
        location = response['results'][0]['geometry']['location']
        latitude, longitude = location['lat'], location['lng']
        print query, latitude, longitude
    else:
        latitude, longitude = None, None
        print query, "<no results>"
    return latitude, longitude

See official documentationfor the complete list of parameters and additional information.

有关完整的参数列表和其他信息,请参阅官方文档

回答by Bemmu

Did some code golfing and ended up with this version. Depending on your need you might want to distinguish some more error conditions.

做了一些代码打高尔夫球,最终得到了这个版本。根据您的需要,您可能希望区分更多错误条件。

import urllib, urllib2, json

def decode_address_to_coordinates(address):
        params = {
                'address' : address,
                'sensor' : 'false',
        }  
        url = 'http://maps.google.com/maps/api/geocode/json?' + urllib.urlencode(params)
        response = urllib2.urlopen(url)
        result = json.load(response)
        try:
                return result['results'][0]['geometry']['location']
        except:
                return None