gpt4 book ai didi

javascript - 谷歌地图上的 getBounds() 不准确?

转载 作者:行者123 更新时间:2023-12-02 14:36:00 25 4
gpt4 key购买 nike

编辑:我的问题实际上与此重复: google maps v3: center of bounds is different from center of the map

我尝试提取谷歌地图边界的中心点。稍后我想通过这个中心点重新定位 map 。然而, map 在南北方向上略有移动。

我在这里遗漏了什么吗?在 plunkr 中,您可以看到 map 在 panTo() 调用后如何移动,尽管它是从 map 中检索的精确中心。

https://plnkr.co/edit/zGy3nUShEFG7fxvKTqa9?p=preview

<!DOCTYPE html>
<html>
<head>
<title>Simple Map</title>
<meta name="viewport" content="initial-scale=1.0">
<meta charset="utf-8">
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script>

var map;
function initMap() {
var centerData = {lat: -34.397, lng: 150.644};
map = new google.maps.Map(document.getElementById('map'), {
center: centerData,
zoom: 8
});

var initialCenterMarker = new google.maps.Marker({
position: centerData,
map: map,
animation:google.maps.Animation.DROP
});


setTimeout(function(){
var bounds = map.getBounds();
var myCenter=bounds.getCenter();

setTimeout(function(){
map.panTo(myCenter);
var newCenterMarker = new google.maps.Marker({
position: myCenter,
map: map,
animation:google.maps.Animation.DROP
});
},2000);

},1000);

}

</script>
<script src="https://maps.googleapis.com/maps/api/js?callback=initMap"
async defer></script>
</body>
</html>

最佳答案

我认为你应该使用map.getCenter而不是bounds.getCenter()

 map.getCenter();

关于javascript - 谷歌地图上的 getBounds() 不准确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37445724/

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