前段時間我寫了一些這樣的練習。不幸的是,它只能在Chrome瀏覽器中正常工作,在其他瀏覽器中存在奇怪的錯誤。距離函數應該在IE9中工作。
我的想法是將drag
監聽器添加到您的標記中,並保留代表路徑的單個多段線,並在拖動標記時更新其路徑。
DEMOhttp://freezoo.alwaysdata.net/freezoomaps/freezoomaps.html
google.maps.event.addListener(tmpNode, 'drag', function(event) {
distance.drawPath();
});
distance.drawPath = function() {
distance.countNodes();
var coords = [];
for (var i = 0; i < distance.nodes.length; i++) {
coords.push(distance.nodes[i].getPosition());
}
distance.line.setPath(coords);
meters = google.maps.geometry.spherical.computeLength(coords);
$("#distance_km").val((meters/1000).toFixed(3));
$("#distance_mi").val((meters/1609.344).toFixed(3));
}
distance.countNodes = function() {
var count = 0;
for (var i = distance.nodes.length - 1; i >= 0; i--) {
if (distance.nodes[i].getMap() == null) {
distance.nodes.splice(i, 1);
}
else {
count++;
}
}
return count;
}
distance.clearNodes = function() {
for(var i = 0; i < distance.nodes.length; i++) {
distance.nodes[i].setMap(null);
}
distance.drawPath();
}
謝謝你的幫助。我可能沒有提到我在js上有點小菜。儘管如此,沒有像大多數那樣不太好。我正在尋找與我的腳本的具體幫助,使用我已經在我鏈接到的js小提琴中使用的變量。我瞭解你正在向我展示的內容我現在試圖弄清楚如何實施到我的項目中。 – Shane