javascript - 使用 google maps api v3 折线捕捉到道路

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

在 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 中,这种简单的方式不起作用。有类似方向服务的东西,但我不知道如何通过我的点绘制多段线,多段线将捕捉到道路。

最佳答案

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

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

关于javascript - 使用 google maps api v3 折线捕捉到道路,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10513360/

相关文章:

javascript - 正则表达式 允许的特殊字符中一次只能使用一个特殊字符 Javascript

android - 当我限制对 SSL/HTTPS 请求的互联网访问时加载 Google map 时出现问题

javascript - 谷歌地图 v3 在 map 上点击设置单个标记点

javascript - 拖动标记后 Ng-map 获取地址

javascript - 谷歌地图 API 获取 lat 和 lng 并替换标记?

google-maps - Angular 2谷歌地图实现

javascript - 浏览器中的特殊鼠标事件 : wheel, 右键单击​​?

javascript - 在TinyMce中,当编辑器处于焦点时如何更改容器的颜色?

javascript - react native ConfigUtils.configFilename 异步不是函数

javascript - 显示卫星图像 - 传单 map (javascript/jquery/css)