簡體   English   中英

如何使用 javascript 和谷歌地圖更改地圖中的位置?

[英]How can I change the location in a map using javascript and google maps?

我正在嘗試使用每個循環來更改地圖的中心以前往不同的地方,但是當我執行代碼時,地圖沒有顯示所有位置,而是地圖顯示第一個和最后一個位置,如果您願意,我將不勝感激可以幫我解決這個問題。

這是代碼:

 var a = new google.maps.LatLng(8.8013, -74.72538); var b = new google.maps.LatLng(1.0619, -73.2558); var c = new google.maps.LatLng(12.5872, -81.7025); var d = new google.maps.LatLng(3.2719, -73.0877); var locations = new Array(a, b, c, d); var mapProp = { center: new google.maps.LatLng(5.0619, -70.2558), zoom: 8, mapTypeId: google.maps.MapTypeId.ROADMAP }; var map = new google.maps.Map(document.getElementById("googleMap"), mapProp); function initialize() { goToLocations(locations); } google.maps.event.addDomListener(window, 'load', initialize); function goToLocations(locations) { $.each(locations, function (index, location) { goToLocation(location); }); } function goToLocation(location) { // here I can not see all locations window.setTimeout(map.setCenter(location), 5000); }
 <html> <body> <script src="http://maps.google.com/maps/api/js?sensor=true" type="text/javascript"> </script> <script src="https://code.jquery.com/jquery-1.10.2.js"></script> <h1>Map that go to different locations </h1> <div id="googleMap" style="width: 500px; height: 380px"></div> </body> </html>

您的function運行良好,它實際上顯示了您的array所有城市。 但是,我們無法看到它。 function執行得如此之快,以至於我們只能看到第一個和最后一個。

嘗試在$.each函數中放置一個Timeout ,它會起作用:

$.each(locations, function (index, location) {
   window.setTimeout(function(){  
      goToLocation(location);
   }, 1000);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM