如何通过google javascript api获取知道place_id的位置坐标

eaw*_*wer 5 google-maps google-maps-api-3 google-geocoder

我知道place_id,我需要知道它的坐标.

我可以 GET https://maps.googleapis.com/maps/api/geocode/json?place_id=ChIJOwE7_GTtwokRFq0uOwLSE9g&key=KEY_GOES_HERE

这给了我这样的东西:

```   "results" : [
      {
         "address_components" : [...],
         "formatted_address" : "New York County, NY, USA",
         "geometry" : {
            "bounds" : {...},
            "location" : {
               "lat" : 40.7830603,
               "lng" : -73.9712488
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {...}
         },
         "partial_match" : true,
         "place_id" : "ChIJOwE7_GTtwokRFq0uOwLSE9g",
         "types" : [...]
      }
   ],
   "status" : "OK"
```
Run Code Online (Sandbox Code Playgroud)

但我需要通过javascript api来做.

我的页面上没有任何地图,只需要获取坐标.

geo*_*zip 9

来自Google Maps Javascript API v3文档中的示例(使用您的place_id):

var infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);

service.getDetails(request, function(place, status) {
  if (status == google.maps.places.PlacesServiceStatus.OK) {
    var marker = new google.maps.Marker({
      map: map,
      position: place.geometry.location
    });
    google.maps.event.addListener(marker, 'click', function() {
      infowindow.setContent(place.name);
      infowindow.open(map, this);
    });
    map.fitBounds(place.geometry.viewport);
  }
});
Run Code Online (Sandbox Code Playgroud)

工作小提琴

代码段:

var geocoder;
var map;

function initialize() {
  var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  var request = {
    placeId: 'ChIJOwE7_GTtwokRFq0uOwLSE9g'
  };

  var infowindow = new google.maps.InfoWindow();
  var service = new google.maps.places.PlacesService(map);

  service.getDetails(request, function(place, status) {
    if (status == google.maps.places.PlacesServiceStatus.OK) {
      var marker = new google.maps.Marker({
        map: map,
        position: place.geometry.location
      });
      google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent(place.name);
        infowindow.open(map, this);
      });
      map.fitBounds(place.geometry.viewport);
    }
  });

}
google.maps.event.addDomListener(window, "load", initialize);
Run Code Online (Sandbox Code Playgroud)
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>
<div id="map_canvas" style="border: 2px solid #3872ac;"></div>
Run Code Online (Sandbox Code Playgroud)


Man*_*ing 5

您不需要地图。这是一个Restful Web服务,返回JSON字符串。在您的应用程序中,您只需要将HTTP请求发送到

GET https://maps.googleapis.com/maps/api/geocode/json?place_id=ChIJOwE7_GTtwokRFq0uOwLSE9g&key=KEY_GOES_HERE

然后解析返回的JSON字符串,并检索其中的latlng。

如果您在Web应用程序中使用Jquery,则可以

    $.ajax({
            type: "GET",
            url: "https://maps.googleapis.com/maps/api/geocode/json?place_id=ChIJOwE7_GTtwokRFq0uOwLSE9g&key=KEY_GOES_HERE",
            dataType: "text", 
            success: function (yourJSONstring){
                var respJson = $.parseJSON(yourJSONstring);  

            // then something like ... 
            // respJson.results[0].geometry.location.lat;
            // respJson.results[0].geometry.location.lng; 


            },
            error: function (xhr) {
            }
        }); 
Run Code Online (Sandbox Code Playgroud)