Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
583 views
in Technique[技术] by (71.8m points)

javascript - Getting waypoints from leaflet routing machine

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: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> &copy; <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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Solution was to not convert to JSON

alert (routeArray[0].latLng.lng);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...