3

我希望用户在 GoogleMaps 上用折线绘制路线。我找到了一种捕捉折线的方法(链接中的示例,下面的代码)。该代码与方向服务一起在道路上绘制折线,唯一的问题是这只适用于 G_TRAVELMODE_DRIVING 但我想使用 G_TRAVELMODE_WALKING 并且当您使用 WALKING 时,您必须在方向的构造函数中提供地图和 div目的。当我这样做时,它会自动删除最后一行,只显示当前行。我尝试了几件事,例如将地图提供为空,或在构造函数中省略地图。我还尝试在构造函数中提供第二张地图,从路线服务中获取折线并将它们显示在右侧地图。但没有任何效果!如果有人可以帮助我,我将不胜感激!

http://econym.org.uk/gmap/example_snappath.htm

if (GBrowserIsCompatible()) {

  var map = new GMap2(document.getElementById("map"));
  map.setCenter(new GLatLng(53.7877, -2.9832),13)
  map.addControl(new GLargeMapControl());
  map.addControl(new GMapTypeControl());
  var dirn = new GDirections();

  var firstpoint = true;
  var gmarkers = [];
  var gpolys = [];
  var dist = 0;


  GEvent.addListener(map, "click", function(overlay,point) {
    // == When the user clicks on a the map, get directiobns from that point to itself ==
    if (!overlay) {
      if (firstpoint) {
        dirn.loadFromWaypoints([point.toUrlValue(6),point.toUrlValue(6)],{getPolyline:true});
      } else {
        dirn.loadFromWaypoints([gmarkers[gmarkers.length-1].getPoint(),point.toUrlValue(6)],{getPolyline:true});
      }
    }
  });

  // == when the load event completes, plot the point on the street ==
  GEvent.addListener(dirn,"load", function() {
    // snap to last vertex in the polyline
    var n = dirn.getPolyline().getVertexCount();
    var p=dirn.getPolyline().getVertex(n-1);
    var marker=new GMarker(p);
    map.addOverlay(marker);
    // store the details
    gmarkers.push(marker);
    if (!firstpoint) {
      map.addOverlay(dirn.getPolyline());
      gpolys.push(dirn.getPolyline());
      dist += dirn.getPolyline().Distance();
      document.getElementById("distance").innerHTML="Path length: "+(dist/1000).toFixed(2)+" km. "+(dist/1609.344).toFixed(2)+" miles.";
    }
    firstpoint = false;
  });

  GEvent.addListener(dirn,"error", function() {
    GLog.write("Failed: "+dirn.getStatus().code);
  });

}
else {
  alert("Sorry, the Google Maps API is not compatible with this browser");
}
4

1 回答 1

0

我知道这是一个旧帖子,但由于没有答案出现,而且我最近一直在研究这种代码(并且让它工作),试着用这个交换 addListener。

GEvent.addListener(map, "click", function(overlay,point) {
    // == When the user clicks on a the map, get directiobns from that point to itself ==
    if (!overlay) {   
      if (firstpoint) {
        dirn1.loadFromWaypoints([point.toUrlValue(6),point.toUrlValue(6)],{ getPolyline:true,travelMode:G_TRAVEL_MODE_WALKING});
      } else {
        dirn1.loadFromWaypoints([gmarkers[gmarkers.length-1].getPoint(),point.toUrlValue(6)],{ getPolyline:true,travelMode:G_TRAVEL_MODE_WALKING});
      }
    }
  });
于 2014-08-10T14:50:52.490 回答