2015-05-18 53 views
0

我工作的谷歌地圖,並在我的地圖我已經建立使用Animating Symbols文章如何在Google地圖上獲取Lat Long移動對象?

移動物體,我的問題是,我想移動物體(動畫符號)的位置。

就像一個符號在點A到B之間移動,所以我想知道每個時間點的位置。

function animateCircle() { 
    var count = 0; 
    window.setInterval(function() { 
     count = (count + 1) % 200; 

     var icons = line.get('icons'); 
     icons[0].offset = (count/2) + '%'; 
     line.set('icons', icons); 

     GetLatLongAtThisPosition(); // i wants to get the current location of here each time   

    }, 20); 
} 
+0

爲什麼有人投票決定關閉這爲_ 「這個問題並不顯得有關編程」 _?似乎對我來說很關鍵 – duncan

+0

@duncan,而不是我,但被標記爲:**尋求調試幫助的問題(「爲什麼不是這個代碼工作?」)必須包括所需的行爲,特定的問題或錯誤以及在問題本身中重現它所需的最短代碼。沒有明確問題陳述的問題對其他讀者無益。請參閱:如何創建一個最小化,完整和可驗證的示例**我猜投票人想要一個小提琴或一些MCV示例 –

+1

用戶不需要調試幫助或演示破損的代碼,他們想知道如何實現一些新功能。 – duncan

回答

1

符號沒有位置,但你可以計算它:

var position = google.maps.geometry.spherical.interpolate(lineCoordinates[0], lineCoordinates[1], (count/200)); 

proof of concept fiddle

function animateCircle() { 
    var infowindow = new google.maps.InfoWindow(); 
    var count = 0; 
    window.setInterval(function() { 
    count = (count + 1) % 200; 

    var icons = line.get('icons'); 
    icons[0].offset = (count/2) + '%'; 
    line.set('icons', icons); 
    var position = google.maps.geometry.spherical.interpolate(lineCoordinates[0], lineCoordinates[1], (count/200)); 
    if (!marker) { 
     marker = new google.maps.Marker({ 
      position: position, 
      map: map 
     }); 
     infowindow.setContent(marker.getPosition().toUrlValue(6)); 
     infowindow.open(map,marker); 
    } else { 
     marker.setPosition(position); 
    } 
    infowindow.setContent(marker.getPosition().toUrlValue(6)); 
    }, 20); 
} 

代碼片段:

// This example adds an animated symbol to a polyline. 
 

 
var line; 
 
var marker; 
 
var lineCoordinates; 
 
var map; 
 

 
function initialize() { 
 
    var mapOptions = { 
 
    center: new google.maps.LatLng(20.291, 153.027), 
 
    zoom: 6, 
 
    mapTypeId: google.maps.MapTypeId.TERRAIN 
 
    }; 
 

 
    map = new google.maps.Map(document.getElementById('map-canvas'), 
 
    mapOptions); 
 

 
    lineCoordinates = [ 
 
    new google.maps.LatLng(22.291, 153.027), 
 
    new google.maps.LatLng(18.291, 153.027) 
 
    ]; 
 

 
    // Define the symbol, using one of the predefined paths ('CIRCLE') 
 
    // supplied by the Google Maps JavaScript API. 
 
    var lineSymbol = { 
 
    path: google.maps.SymbolPath.CIRCLE, 
 
    scale: 8, 
 
    strokeColor: '#393' 
 
    }; 
 

 
    // Create the polyline and add the symbol to it via the 'icons' property. 
 
    line = new google.maps.Polyline({ 
 
    path: lineCoordinates, 
 
    icons: [{ 
 
     icon: lineSymbol, 
 
     offset: '100%' 
 
    }], 
 
    map: map 
 
    }); 
 

 
    animateCircle(); 
 
} 
 

 
// Use the DOM setInterval() function to change the offset of the symbol 
 
// at fixed intervals. 
 
function animateCircle() { 
 
    var infowindow = new google.maps.InfoWindow(); 
 
    var count = 0; 
 
    window.setInterval(function() { 
 
    count = (count + 1) % 200; 
 

 
    var icons = line.get('icons'); 
 
    icons[0].offset = (count/2) + '%'; 
 
    line.set('icons', icons); 
 
    var position = google.maps.geometry.spherical.interpolate(lineCoordinates[0], lineCoordinates[1], (count/200)); 
 
    if (!marker) { 
 
     marker = new google.maps.Marker({ 
 
     position: position, 
 
     map: map 
 
     }); 
 
     infowindow.setContent(marker.getPosition().toUrlValue(6)); 
 
     infowindow.open(map, marker); 
 
    } else { 
 
     marker.setPosition(position); 
 
    } 
 
    infowindow.setContent(marker.getPosition().toUrlValue(6)); 
 
    }, 20); 
 
} 
 

 
google.maps.event.addDomListener(window, 'load', initialize);
html, 
 
body, 
 
#map-canvas { 
 
    height: 500px; 
 
    width: 500px; 
 
    margin: 0px; 
 
    padding: 0px 
 
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry&ext=.js"></script> 
 
<div id="map-canvas" style="border: 2px solid #3872ac;"></div>

+0

非常感謝你親愛的,它爲我工作:) –

+0

可否請您將我的問題加1標記。 –

+0

它只適用於2點。那麼(N> 2)分呢? – w35l3y

-2

不幸的是,我不認爲您可以通過Google地圖API獲得Lat Long的SymbolPath。

如果console.logicons[0]你會看到iconobject只有路徑規模則strokeColor

console.log(icons[0].icon) 

{ 
    path: 0, 
    scale: 8, 
    strokeColor: "#393" 
} 
相關問題