Google地图路径未使用epoly.js跨越国际日期

时间:2015-01-20 08:36:25

标签: javascript jquery google-maps google-polyline

我正在通过在循环中使用来自epoly.js的.GetPointDistance来使标记从A点移动到B点。只要最短距离不越过Intl Dateline,它就可以正常工作。如果是这样的话,比如从旧金山前往东京,它会向东行进很长一段路而不是穿越太平洋(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);
}

1 个答案:

答案 0 :(得分:1)

如果您在国际日期变更线上添加一个点,则可以使用

example

包括以下几点:

<point lat="37.77493" lng="-122.419416"/>
<point lat="39.614028" lng="-179.906751"/>
<point lat="39.904211" lng="116.407395"/>