Google 地图路径未使用 epoly.js 跨越国际日期变更线
Google map path doesn't cross International Dateline using epoly.js
我在循环中使用 epoly.js 中的 .GetPointDistance 使标记从点 A 移动到点 B。只要最短距离不跨越国际日期变更线,它就可以正常工作。如果是这样,就像从旧金山到东京一样,它会向东行进很长一段时间而不是穿越太平洋(Google 折线将正确绘制)
这是我的代码
function myLoop () {
setTimeout(function () {
var point = flightPath.GetPointAtDistance(newDistance);
marker.setPosition(point);
if (newDistance < legDistance) {
myLoop();
} else {
if (testArray.length > 0) {
flightPathShadow.setMap(map);
flightPath.setMap(map);
x = testArray.shift();
newDistance = 0;
loopWrapper();
}
}
oldDistance = newDistance;
newDistance = newDistance + travelDistance;
totalDistance = (totalDistance + travelDistance);
var milesDistance = totalDistance*0.000621371;
milesDistance = Math.round(milesDistance);
milesDistance = addCommas(milesDistance);
$("#dashboard").html("<h4>" + milesDistance + " miles traveled</h4>");
}, travelTime)
}
这是epoly.js
中的函数
google.maps.Polygon.prototype.GetPointAtDistance = function(metres) {
if (metres == 0) return this.getPath().getAt(0);
if (metres < 0) return null;
if (this.getPath().getLength() < 2) return null;
var dist=0;
var olddist=0;
for (var i=1; (i < this.getPath().getLength() && dist < metres); i++) {
olddist = dist;
dist += this.getPath().getAt(i).distanceFrom(this.getPath().getAt(i-1));
}
if (dist < metres) {
return null;
}
var p1= this.getPath().getAt(i-2);
var p2= this.getPath().getAt(i-1);
var m = (metres-olddist)/(dist-olddist);
return new google.maps.LatLng( p1.lat() + (p2.lat()-p1.lat())*m, p1.lng() + (p2.lng()-p1.lng())*m);
}
如果您在国际日期变更线上添加一个点,就可以了
包括以下几点:
<point lat="37.77493" lng="-122.419416"/>
<point lat="39.614028" lng="-179.906751"/>
<point lat="39.904211" lng="116.407395"/>
我在循环中使用 epoly.js 中的 .GetPointDistance 使标记从点 A 移动到点 B。只要最短距离不跨越国际日期变更线,它就可以正常工作。如果是这样,就像从旧金山到东京一样,它会向东行进很长一段时间而不是穿越太平洋(Google 折线将正确绘制) 这是我的代码
function myLoop () {
setTimeout(function () {
var point = flightPath.GetPointAtDistance(newDistance);
marker.setPosition(point);
if (newDistance < legDistance) {
myLoop();
} else {
if (testArray.length > 0) {
flightPathShadow.setMap(map);
flightPath.setMap(map);
x = testArray.shift();
newDistance = 0;
loopWrapper();
}
}
oldDistance = newDistance;
newDistance = newDistance + travelDistance;
totalDistance = (totalDistance + travelDistance);
var milesDistance = totalDistance*0.000621371;
milesDistance = Math.round(milesDistance);
milesDistance = addCommas(milesDistance);
$("#dashboard").html("<h4>" + milesDistance + " miles traveled</h4>");
}, travelTime)
}
这是epoly.js
中的函数google.maps.Polygon.prototype.GetPointAtDistance = function(metres) {
if (metres == 0) return this.getPath().getAt(0);
if (metres < 0) return null;
if (this.getPath().getLength() < 2) return null;
var dist=0;
var olddist=0;
for (var i=1; (i < this.getPath().getLength() && dist < metres); i++) {
olddist = dist;
dist += this.getPath().getAt(i).distanceFrom(this.getPath().getAt(i-1));
}
if (dist < metres) {
return null;
}
var p1= this.getPath().getAt(i-2);
var p2= this.getPath().getAt(i-1);
var m = (metres-olddist)/(dist-olddist);
return new google.maps.LatLng( p1.lat() + (p2.lat()-p1.lat())*m, p1.lng() + (p2.lng()-p1.lng())*m);
}
如果您在国际日期变更线上添加一个点,就可以了
包括以下几点:
<point lat="37.77493" lng="-122.419416"/>
<point lat="39.614028" lng="-179.906751"/>
<point lat="39.904211" lng="116.407395"/>