【问题标题】:polyline snap to road using google maps api v3使用谷歌地图 api v3 将折线捕捉到道路
【发布时间】:2012-05-17 19:22:24
【问题描述】:

在 google maps api v2 中很容易,

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;

// == When the user clicks on a the map, get directiobns from that point to itself ==

gmarkers.push(new google.maps.LatLng(53.7877, -2.9832));
gmarkers.push(new google.maps.LatLng(53.9007, -2.9832));
gmarkers.push(new GLatLng(53.600, -2.700));



for (var i = 0; i < gmarkers.length-1; i++) {
    console.log(gmarkers[i]);
                dirn.loadFromWaypoints([gmarkers[i].toUrlValue(6),gmarkers[i+1].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();
            map.addOverlay(dirn.getPolyline());
            gpolys.push(dirn.getPolyline());
            dist += dirn.getPolyline().getDistance();
            document.getElementById("distance").innerHTML="Path length: "+(dist/1000).toFixed(2)+" km. "+(dist/1609.344).toFixed(2)+" miles.";
           });
    GEvent.addListener(dirn,"error", function() {
        GLog.write("Failed: "+dirn.getStatus().code);
    });
console.log(dirn);

在 google api V3 中,这种简单的方式不起作用。有类似方向服务的东西,但我不知道如何通过我的点绘制折线,并且折线将被捕捉到道路。

【问题讨论】:

    标签: javascript google-maps google-maps-api-3


    【解决方案1】:

    您在使用路线服务时走在了正确的轨道上。这是示例代码:

    var map, path = new google.maps.MVCArray(),
        service = new google.maps.DirectionsService(), poly;
    
    function Init() {
      var myOptions = {
        zoom: 17,
        center: new google.maps.LatLng(37.2008385157313, -93.2812106609344),
        mapTypeId: google.maps.MapTypeId.HYBRID,
        mapTypeControlOptions: {
          mapTypeIds: [google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.HYBRID,
              google.maps.MapTypeId.SATELLITE]
        },
        disableDoubleClickZoom: true,
        scrollwheel: false,
        draggableCursor: "crosshair"
      }
    
      map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
      poly = new google.maps.Polyline({ map: map });
      google.maps.event.addListener(map, "click", function(evt) {
        if (path.getLength() === 0) {
          path.push(evt.latLng);
          poly.setPath(path);
        } else {
          service.route({
            origin: path.getAt(path.getLength() - 1),
            destination: evt.latLng,
            travelMode: google.maps.DirectionsTravelMode.DRIVING
          }, function(result, status) {
            if (status == google.maps.DirectionsStatus.OK) {
              for (var i = 0, len = result.routes[0].overview_path.length;
                  i < len; i++) {
                path.push(result.routes[0].overview_path[i]);
              }
            }
          });
        }
      });
    }
    

    另外,请参阅我的工作示例:http://people.missouristate.edu/chadkillingsworth/mapsexamples/snaptoroad.htm

    【讨论】:

    • @chad-killingsworth 所以如果我没看错的话,你可以绕过最大 8 个航点的限制,通过每次点击调用 DirectionsService,然后将所有这些结果存储在一个数组中?假设我想在每 1 英里的增量处自动显示一个标记,类似于 gmaps-pedometer.com;知道如何使用 API 做到这一点吗?
    • 对不起,忽略上述问题的后半部分;在这里找到了答案:stackoverflow.com/questions/2698112/… .. 不是很简单,看起来需要为 v3 重新设计,但看起来可行...
    • 乍得 - 也许我遗漏了一些东西,但您的工作示例似乎不允许绘制任何线条(左键单击移动地图视点;右键单击会显示浏览器上下文菜单)。
    • 如果我双击地图,线条画得很好。很酷的地图,干得好。
    • 我正在尝试效仿你的榜样,但没有成功..stackoverflow.com/questions/29044948/…
    【解决方案2】:

    最好使用 Snap to Roads API,并将 interpolate 设置为 true 以获得完美的捕捉折线。 https://developers.google.com/maps/documentation/roads/snap

    【讨论】:

    • 如何在静态地图url中添加标记(例如&markers=color:red|label:D|54.5661310,-1.2505580)?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    • 2015-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-02
    相关资源
    最近更新 更多