2012-04-04 117 views

回答

28

是的。只要使用谷歌地理编码和Places API的https://developers.google.com/maps/documentation/geocoding/https://developers.google.com/maps/documentation/places/

例(来自here派生):

var geocoder; 

function initialize() { 
    geocoder = new google.maps.Geocoder(); 
} 

function codeLatLng(lat, lng) { 
    var latlng = new google.maps.LatLng(lat, lng); 
    geocoder.geocode({ 
    'latLng': latlng 
    }, function (results, status) { 
    if (status === google.maps.GeocoderStatus.OK) { 
     if (results[1]) { 
     console.log(results[1]); 
     } else { 
     alert('No results found'); 
     } 
    } else { 
     alert('Geocoder failed due to: ' + status); 
    } 
    }); 
} 

google.maps.event.addDomListener(window, 'load', initialize); 
+0

你能提供一个例子吗? – Jackson 2015-05-01 23:43:50

+2

https://developers.google.com/maps/documentation/javascript/examples/geocoding-reverse – rijndael 2015-05-04 21:24:55

+1

应该使用results [0]作为第一个结果,而不是结果[1]。 – IceWarrior353 2017-07-13 23:21:16

10

有可能访问此信息发送简单的GET请求地理编码端点。

例如打https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=API_KEY会产生以下反应:

{ 
    "results" : [ 
     { 
     "address_components" : [ 
      { 
       "long_name" : "277", 
       "short_name" : "277", 
       "types" : [ "street_number" ] 
      }, 
      { 
       "long_name" : "Bedford Avenue", 
       "short_name" : "Bedford Ave", 
       "types" : [ "route" ] 
      }, 
      { 
       "long_name" : "Williamsburg", 
       "short_name" : "Williamsburg", 
       "types" : [ "neighborhood", "political" ] 
      }, 
      { 
       "long_name" : "Brooklyn", 
       "short_name" : "Brooklyn", 
       "types" : [ "sublocality", "political" ] 
      }, 
      { 
       "long_name" : "Kings", 
       "short_name" : "Kings", 
       "types" : [ "administrative_area_level_2", "political" ] 
      }, 
      { 
       "long_name" : "New York", 
       "short_name" : "NY", 
       "types" : [ "administrative_area_level_1", "political" ] 
      }, 
      { 
       "long_name" : "United States", 
       "short_name" : "US", 
       "types" : [ "country", "political" ] 
      }, 
      { 
       "long_name" : "11211", 
       "short_name" : "11211", 
       "types" : [ "postal_code" ] 
      } 
     ], 
     "formatted_address" : "277 Bedford Avenue, Brooklyn, NY 11211, USA", 
     "geometry" : { 
      "location" : { 
       "lat" : 40.714232, 
       "lng" : -73.9612889 
      }, 
      "location_type" : "ROOFTOP", 
      "viewport" : { 
       "northeast" : { 
        "lat" : 40.7155809802915, 
        "lng" : -73.9599399197085 
       }, 
       "southwest" : { 
        "lat" : 40.7128830197085, 
        "lng" : -73.96263788029151 
       } 
      } 
     }, 
     "place_id" : "ChIJd8BlQ2BZwokRAFUEcm_qrcA", 
     "types" : [ "street_address" ] 
     }, 

    ... Additional results[] ... 

https://developers.google.com/maps/documentation/geocoding/intro#reverse-example