gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:59:47 25 4
gpt4 key购买 nike

我知道 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"
```

但我需要通过 javascript api 来完成。

我的页面上没有任何 map ,只需要获取坐标即可。

最佳答案

来自 the example in the Google Maps Javascript API v3 documentation (使用您的 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);
}
});

working fiddle

代码片段:

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);
html,
body,
#map_canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=places&key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map_canvas" style="border: 2px solid #3872ac;"></div>

关于google-maps - 如何通过google javascript api知道place_id获取位置坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29659688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com