Google Maps APIをGoogleMaps Pythonライブラリとともに使用して、アドレスの一部または全部が指定されている場合に緯度/経度をジオコード化しようとしていますたとえば、私は都市/州を使用していますが、私はこの行を使用するために必要な郵便番号のデータセットも持っています)。Pythonでジオコード結果リストからlat/lonを抽出する(Google Maps API)
import googlemaps
gmaps = googlemaps.Client(key=[insert API key here])
geocode_result = gmaps.geocode('Sacramento, CA')
print(geocode_result)
Result:[{u'geometry': {u'location_type': u'APPROXIMATE', u'bounds': {u'northeast': {u'lat': 38.685507, u'lng': -121.325705}, u'southwest': {u'lat': 38.437574, u'lng': -121.56012}}, u'viewport': {u'northeast': {u'lat': 38.685507, u'lng': -121.325705}, u'southwest': {u'lat': 38.437574, u'lng': -121.56012}}, u'location': {u'lat': 38.5815719, u'lng': -121.4943996}}, u'address_components': [{u'long_name': u'Sacramento', u'types': [u'locality', u'political'], u'short_name': u'Sacramento'}, {u'long_name': u'Sacramento County', u'types': [u'administrative_area_level_2', u'political'], u'short_name': u'Sacramento County'}, {u'long_name': u'California', u'types': [u'administrative_area_level_1', u'political'], u'short_name': u'CA'}, {u'long_name': u'United States', u'types': [u'country', u'political'], u'short_name': u'US'}], u'place_id': u'ChIJ-ZeDsnLGmoAR238ZdKpqH5I', u'formatted_address': u'Sacramento, CA, USA', u'types': [u'locality', u'political']}]
私の問題は、このリストから適切な緯度/経度値を抽出する方法がわかりません。私は(this question on SOへの回答から取られた)次のコードを使用して、リストを解析してみた:
import operator
thirditem=operator.itemgetter(3)(geocode_result)
print(thirditem)
私はこれを実行すると、私はインデックスが範囲外であると言ってはIndexErrorを取得します。私はデバッガでも実行しましたが、私は追加の情報なしで同じエラーが発生します。私は周りを探索し、他のSOの質問を見て、しかし私はまだ問題がどこにあるか分からない。
また、コード例をthis tutorialで使用しようとしましたが、実行しようとすると答えが「0」になりますが、残念ながらIndexErrorよりもあまり役に立ちません。
私の目標は、ここから適切なlat/lon値を解析して、このベースマップスクリプトに動的に挿入できるようにすることです。値は、現在ハードコーディングされているが、最終的に私は値llcrnrlon、llcrnrlat、urcrnrlon、urcrnrlat、lat_0のための変数を使用できるようにしたいと思い、そしてlon_0:
map = Basemap(projection='merc',
# with high resolution,
resolution= 'h',
# And threshold 100000
area_thresh = 100000.0,
# Centered on these coordinates
lat_0=37, lon_0=119,
#and using these corners to specify the lower left lon/lat and upper right lon/lat of the graph)
llcrnrlon=-130, llcrnrlat=30,
urcrnrlon=-110, urcrnrlat=45)
は、私はすべてのSUPER新たなんですこれは私が見ていない簡単な答えかもしれない。どんな助けも歓迎です!ありがとう。
なぜdownvote?私はちょうど勉強しようとしています..... – happygostacie