前段时间我写了一些这样的练习。不幸的是,它只能在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