Get definite City name in Google maps reverse geocoding

前端 未结 3 619
野的像风
野的像风 2021-01-03 01:05

Using the Google Maps Geocoding API, i\'m able to get the formatted address for a particular coordinate. To get the exact city name, I\'m doing the following:



        
3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-03 02:04

    You need to look at the type of the result, not the absolute index in the array of results. Iterate through the results array looking for the entry which has the appropriate type. Looks like that would be:

    • locality indicates an incorporated city or town political entity

    But data may vary with region.

    related question: Grabbing country from google geocode jquery

    Looks like you want the entry with both the 'locality' and the 'political' types:

    {
       "long_name" : "Mumbai",
       "short_name" : "Mumbai",
       "types" : [ "locality", "political" ]
    }
    

提交回复
热议问题