首页 文章

在Google Maps JavaScript API中拖动路线(即新航点)时触发某些事件

提问于
浏览
-1

基本上我希望能够从Google Map 视图中提取路线之间的起点,终点和任何航点的纬度和经度信息 .

目前我正在使用这个例子https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-directions

在测试时,我只是将我需要的信息存储在全局范围内名为“route”的变量中 .

var saveRoute = function (directionsDisplay) {
    var final_waypoints=[], waypoints;
    var route_legs = directionsDisplay.directions.routes[0].legs[0];
    if(!route_legs) {
      return
    }
    route.start = {'lat': route_legs.start_location.lat(), 'lng': route_legs.start_location.lng()}
    route.end = {'lat': route_legs.end_location.lat(), 'lng': route_legs.end_location.lng()}
    var waypoints = route_legs.via_waypoints 

    for(var i=0;i<waypoints.length;i++) {
      final_waypoints[i] = [waypoints[i].lat(), waypoints[i].lng()] 
    }
    route.waypoints = final_waypoints;
}

我能够通过在'setupPlaceChangedListener'中调用我自己的函数来获取我最初需要的信息,该函数从DirectionsRenderer对象读取信息并将其保存在变量中 . 但是,我想允许用户拖动显示的路线,允许他们更改确切的路线(不一定是开始和目的地) .

最初我希望我可以触发一些事件,以便在发生变化时重新运行我的功能 . 'dragend'功能仅在拖动 Map 时有效,并且仅在拖动包含新航点时才执行任何操作 .

有没有办法做到这一点?我这样滥用Google Maps API吗?有一个更好的方法吗?

1 回答

  • 0

    Draggable Directions的文档:

    由于可拖动方向被修改并呈现在客户端,您可能希望监视和处理DirectionsRenderer上的directions_changed事件,以便在用户修改显示的方向时收到通知 .

相关问题