2012-01-25 61 views
2

我試圖沿着具有函數的路線移動單個標記。我想聲明的標記在全球範圍內,但它似乎沒有工作...我的其他全局變量做...Google標記範圍

var marker = new google.maps.Marker({map: map});  

     function playTrip(i) { 
      if (i < numRows) { 
       var lat_value = data.getValue(i,3); 
       var lon_value = data.getValue(i,4); 
       var loc = new google.maps.LatLng(lat_value, lon_value); 
       marker.setPosition(loc); 
       i++; 
       setTimeout("playTrip("+(i)+")",20); 
      } 
     } 
+0

你有一個鏈接到住的代碼?以這種方式進行調試要容易得多。 –

回答

1

它可以使用全局變量來存儲標記,也映射。

這是代碼,它將動態地更改地圖和標記位置。

<html> 
    <head> 

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>  
<script type="text/javascript">  


    var marker = null, map = null; 
     var lon = 14.7730;  

     function playTrip() { 
      if (lon < 400){ 
       lon += 1 
       var loc = new google.maps.LatLng(56.8848, lon); 
       marker.setPosition(loc); 
       setTimeout("playTrip()", 3000); 
       //bounds.extend(loc); 
       map.setCenter(loc);    
      } 
      console.log(lon);    
     } 

     (function() { 
     window.onload = function(){ 
      // Creating a LatLng object containing the coordinate for the center of the map 
      var latlng = new google.maps.LatLng(56.83, 15.16); 
      // Creating an object literal containing the properties we want to pass to the map 
      var options = { 
      zoom: 7, 
      center: latlng, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
      }; 
      // Calling the constructor, thereby initializing the map 
      map = new google.maps.Map(document.getElementById('google_map'), options); 

      marker = new google.maps.Marker({ 
      position: new google.maps.LatLng(56.8848, 14.7730), 
      map: map, 
      title: 'My workplace', 
      clickable: false, 
      icon: 'http://google-maps-icons.googlecode.com/files/factory.png' 
      }); 

      playTrip();     
     } 
     })(); 

</script> 
    <body> 
    <div id="google_map" style="width:500px;height:400px;"></div> 
    </body> 
</html> 

我創建了一個頁面here

+0

通過將標記構造函數移動到我的地圖初始化函數中來實現它。 – justinwp