More draggable routes work

This commit is contained in:
Richard Fairhurst 2014-01-23 11:38:01 +00:00
parent edd6235b45
commit e825f8b264
4 changed files with 19 additions and 12 deletions

View file

@ -56,6 +56,7 @@ OSM.Routing=function(map,name,jqSearch) {
r.route_from=null; // null=unset, false=awaiting response, [lat,lon]=geocoded r.route_from=null; // null=unset, false=awaiting response, [lat,lon]=geocoded
r.route_to=null; // | r.route_to=null; // |
r.awaitingGeocode=false;// true if the user has requested a route, but we're waiting on a geocode result r.awaitingGeocode=false;// true if the user has requested a route, but we're waiting on a geocode result
r.awaitingRoute=false; // true if we've asked the engine for a route and are waiting to hear back
r.viaPoints=[]; // not yet used r.viaPoints=[]; // not yet used
r.polyline=null; // Leaflet polyline object r.polyline=null; // Leaflet polyline object
@ -107,7 +108,7 @@ OSM.Routing=function(map,name,jqSearch) {
r.updateMarkers(field.id); r.updateMarkers(field.id);
if (r.awaitingGeocode) { if (r.awaitingGeocode) {
r.awaitingGeocode=false; r.awaitingGeocode=false;
r.requestRoute(); r.requestRoute(true);
} }
}; };
@ -118,6 +119,8 @@ OSM.Routing=function(map,name,jqSearch) {
var ll=r.map.mouseEventToLatLng(e.originalEvent); var ll=r.map.mouseEventToLatLng(e.originalEvent);
// *** ^^^ this is slightly off - we need to work out the latLng of the tip // *** ^^^ this is slightly off - we need to work out the latLng of the tip
r.createMarker(ll,id); r.createMarker(ll,id);
r[id.replace('marker','route')]=[ll.lat,ll.lng];
r.requestRoute(true);
// update to/from field // update to/from field
}; };
r.createMarker=function(latlng,id) { r.createMarker=function(latlng,id) {
@ -135,17 +138,20 @@ OSM.Routing=function(map,name,jqSearch) {
r.markerDragged=function(e) { r.markerDragged=function(e) {
// marker has been dragged // marker has been dragged
if (e.type=='drag' && !r.chosenEngine.draggable) return; if (e.type=='drag' && !r.chosenEngine.draggable) return;
// *** also return if e.type=='drag' and not long enough since last request returned if (e.type=='drag' && r.awaitingRoute) return;
// *** but always do if e.type=='dragend' var id=e.target.options.name;
console.log(e.target.options.name); var ll=e.target.getLatLng();
console.log(e.target.getLatLng()); r[id.replace('marker','route')]=[ll.lat,ll.lng];
r.requestRoute(e.type=='dragend');
// update to/from field
}; };
// Route-fetching UI // Route-fetching UI
r.requestRoute=function() { r.requestRoute=function(final) {
if (r.route_from && r.route_to) { if (r.route_from && r.route_to) {
r.chosenEngine.getRoute(true,[r.route_from,r.route_to]); r.awaitingRoute=true;
r.chosenEngine.getRoute(final,[r.route_from,r.route_to]);
// then, when the route has been fetched, it'll call the engine's gotRoute function // then, when the route has been fetched, it'll call the engine's gotRoute function
} else if (r.route_from==false || r.route_to==false) { } else if (r.route_from==false || r.route_to==false) {
// we're waiting for a Nominatim response before we can request a route // we're waiting for a Nominatim response before we can request a route
@ -157,7 +163,8 @@ OSM.Routing=function(map,name,jqSearch) {
r.setPolyline=function(line) { r.setPolyline=function(line) {
if (r.polyline) map.removeLayer(r.polyline); if (r.polyline) map.removeLayer(r.polyline);
r.polyline=L.polyline(line, ROUTING_POLYLINE).addTo(r.map); r.polyline=L.polyline(line, ROUTING_POLYLINE).addTo(r.map);
r.map.fitBounds(r.polyline.getBounds()); // r.map.fitBounds(r.polyline.getBounds());
// *** ^^^ we only want to do this for geocode-originated routes
}; };
// Take directions and write them out // Take directions and write them out
@ -222,7 +229,7 @@ OSM.Routing=function(map,name,jqSearch) {
script.src = url+r.name+".gotRoute"+num; script.src = url+r.name+".gotRoute"+num;
document.body.appendChild(script); document.body.appendChild(script);
}; };
r['gotRoute'+num]=function(data) { list[num].gotRoute(r,data); }; r['gotRoute'+num]=function(data) { r.awaitingRoute=false; list[num].gotRoute(r,data); };
} }
select.append("<option value='"+i+"'>"+list[i].name+"</option>"); select.append("<option value='"+i+"'>"+list[i].name+"</option>");
} }

View file

@ -5,7 +5,7 @@
OSM.RoutingEngines.list.push({ OSM.RoutingEngines.list.push({
name: 'Foot (CloudMade)', name: 'Foot (CloudMade)',
draggable: true, draggable: false,
CM_SPRITE_MAP: { CM_SPRITE_MAP: {
"C": 1, "C": 1,
"TL": 7, "TL": 7,

View file

@ -7,7 +7,7 @@
OSM.RoutingEngines.list.push({ OSM.RoutingEngines.list.push({
name: 'Bicycle (MapQuest Open)', name: 'Bicycle (MapQuest Open)',
draggable: true, draggable: false,
_hints: {}, _hints: {},
MQ_SPRITE_MAP: { MQ_SPRITE_MAP: {
0: 1, // straight 0: 1, // straight

View file

@ -37,6 +37,6 @@ OSM.RoutingEngines.list.push({
if (instCodes[0]!=15) { instText+=s[1] ? "<b>"+s[1]+"</b>" : "(unnamed)"; } if (instCodes[0]!=15) { instText+=s[1] ? "<b>"+s[1]+"</b>" : "(unnamed)"; }
steps.push([line[s[3]], s[0].split('-')[0], instText, s[2]]); steps.push([line[s[3]], s[0].split('-')[0], instText, s[2]]);
} }
router.setItinerary({ steps: steps }); if (steps.length) router.setItinerary({ steps: steps });
} }
}); });