gpt4 book ai didi

javascript - 在不指定纬度和经度的情况下将谷歌地图置于地点 ID 的中心?

转载 作者:行者123 更新时间:2023-11-29 16:54:56 25 4
gpt4 key购买 nike

如何根据 placeID 将谷歌地图居中,而不在 map 对象中明确设置纬度和经度?

我见过的所有示例都有一个值集,例如

var pyrmont = new google.maps.LatLng(-33.8665433,151.1956316);

表格:developers.google.com

您确定可以根据 ID 获取某个地点的详细信息,然后使用纬度和经度来使 map 居中吗?

function initializeGoogleMaps() {
jQuery('.site-content').prepend('<div id=\"map-container\"><div id=\"map-canvas\"></div></div>');

var request = {
placeId: 'ChIJzXBGLkQFdkgRoyT2MIxgWGw'
};

function callback(place, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
}
}

var map = new google.maps.Map(document.getElementById('map-canvas'), {
center: {
lat: 51.509526,
lng: -0.156314
},
zoom: 15
});

service = new google.maps.places.PlacesService(map);
service.getDetails(request, callback);
}

最佳答案

您可以使用地点服务返回的值(或标记的位置)来设置 map 的中心:

proof of concept fiddle

  function callback(place, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
map.setCenter(marker.getPosition());
}
}

var map = new google.maps.Map(document.getElementById('map-canvas'), {
zoom: 15
});

代码片段:

function initializeGoogleMaps() {
var request = {
placeId: 'ChIJzXBGLkQFdkgRoyT2MIxgWGw'
};

function callback(place, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
map.setCenter(marker.getPosition());
}
}

var map = new google.maps.Map(document.getElementById('map-canvas'), {
zoom: 15
});

service = new google.maps.places.PlacesService(map);
service.getDetails(request, callback);
}
google.maps.event.addDomListener(window, 'load', initializeGoogleMaps);
html,
body,
#map-canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>
<div id="map-canvas"></div>

关于javascript - 在不指定纬度和经度的情况下将谷歌地图置于地点 ID 的中心?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32974265/

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