I'm trying to get the waypoints from a route (leaflet routing machine) as latitude and longitude so I can write these out to a database. However when I call getwaypoints I also get additional data about the route that I don't want.
How do i get just the lat/long as JSON so I can write it out to a db?
var map = L.map('map').setView([-27.54, 152.9], 10);
L.tileLayer('http://{s}.basemaps.cartocdn.com/light_all//{z}/{x}/{y}.png', {
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> © <a href="http://cartodb.com/attributions">CartoDB</a>',
maxZoom: 19
}).addTo(map);
var routeControl = L.Routing.control({waypoints: [
L.latLng(-27.38851, 153.11606),
L.latLng(-27.47577, 153.01693)
]}).addTo(map);
var routeArray = new Array();
routeArray = routeControl.getWaypoints();
alert (JSON.stringify(routeArray));
Output
[{"options":{"allowUTurn":false},"latLng":{"lat":-27.38851,"lng":153.11606},"_initHooksCalled":true},{"options":{"allowUTurn":false},"latLng":{"lat":-27.47577,"lng":153.01693},"_initHooksCalled":true}]
Please see example fiddle: http://jsfiddle.net/drcccx91/8/
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…