2012-01-25 13 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

マーカーコンストラクタをmy map intialize関数に移動することで動作するようになりました。 – justinwp

関連する問題