繁体   English   中英

如何使Google地图跟随新的标记位置?

[英]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>

尝试这个:

//Assuming that you have something like this
map = new google.maps.Map(document.getElementById('map'));
map.setCenter(new google.maps.LatLng(mylat,mylong));

另外,在Google这个主题上,有大量可用的文章和示例。 同时在此页面上搜索方法列表。 也查看这篇文章

您可以使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM