2015-12-02 10 views
2

我正在使用nominatim传单路由。路由完美地按照我的要求运行 - 用户可以在搜索框中输入目标位置,并且地图显示两点之间的路线,如下图所示。 enter image description here传单路由中的目的地坐标

但我想获得目标位置的坐标。有什么办法可以做到这一点?以下是代码示例我如何将地图添加到我的页面。

var map = L.map('map').setView([60.4500, 22.2667], 8); 


    L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', { 
     attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors' 
    }).addTo(map) 


    L.Routing.control({ 
     waypoints: [ 
      //L.latLng(60.323935,22.344035) 

     ], 

     geocoder: L.Control.Geocoder.nominatim() 


    }).addTo(map); 

回答

2

看看RoutingResultEvent。每次成功计算路线时都会调用它。处理程序将收到的事件包含用于路线的路标。

所以基本上

var x = L.Routing.control({ 
    // YOUR STUFF 
    geocoder: L.Control.Geocoder.nominatim() 
}).addTo(map); 
x.on("routesfound", function(e) { 
    var waypoints = e.waypoints || []; 
    var destination = waypoints[waypoints.length - 1]; // there you have the destination point between your hands 

}); 
+0

我试过上面的方法。它产生一个数组 - [object,object],但不是实际的坐标。航点也会导致[object,object],[object,object]。我怎样才能得到它的实际坐标? – user3303274

+0

你的意思是,e.waypoints返回[object,object]?然后,航点[waypoints.length - 1]将返回一个对象,并且这个对象将包含你想要的东西,我猜。 http://www.liedman.net/leaflet-routing-machine/api/#l-routing-waypoint。在我的例子中,destination.latLng会返回lat/lng –

0

你可以使用:

routeControl.on("routesfound", function(e) { 
    coordinates=e.routes[0].coordinates; 
    destination=coordinates[coordinates.length-1]; 
}); 

你有坐标,航点是不一样的坐标,你正在寻找的是对的坐标找到的路线,而不是你要求的航点,那么你可以把坐标.lenght-1,那里你会有你想要的