![](/img/trans.png)
[英]How can I prevent google maps from refreshing while setting the marker to a new position?
[英]How can I make google maps follow the new marker position?
我有经度和纬度变量不断更新为新位置,并使用它们在地图上为标记设置新位置。 当标记开始移出屏幕时,我希望地图能够一直跟随它。 我怎样才能做到这一点? 这是我的代码:
<script>
function initialize() {
var Longitude = //coordinates from server
var Latitude = //coordinates from server
var latlng = new google.maps.LatLng(Latitude,Longitude);
marker.setPosition(latlng);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
您可以使用Map类的setCenter
方法。 首次初始化地图时以及每次标记更改位置时,都应使用它。
<script>
var map; // you need to keep a reference to the map object in the global scope so you can call setCenter on it after initialization
function initialize() {
// initialize your map
map = new google.maps.Map(document.getElementById("map_canvas"));
var Longitude = //coordinates from server
var Latitude = //coordinates from server
var latlng = new google.maps.LatLng(Latitude,Longitude);
marker.setPosition(latlng);
map.setCenter(latlng); // center your map on the marker location
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.