If you want to open a new page with OSRM, centered on and with starting point as the marker coordinates from your map, you would just need to find the appropriate URL to link to.
It looks like an URL template like this works:
"http://map.project-osrm.org/?z=18¢er={lat}%2C{lng}&loc={lat}%2C{lng}&loc={lat}%2C{lng}&hl=en&ly=&alt=&df=&srv="
With {lat}
and {lng}
in decimal degrees of course.
It will center the view on your coordinates and set the start and end points as those coordinates. It looks like you have to specify the end point, and if it is the same as the start point, OSRM map will automatically zoom to max zoom (18)…
Demo: http://jsfiddle.net/ve2huzxw/109/
If you want to integrate the routing within your map, rather than redirecting to OSRM, you would probably be interested in Leaflet Routing Machine plugin.
Other possible plugins on http://leafletjs.com/plugins.html#routing
EDIT:
Replacing the {lat}
and {lng}
in the URL template to include the proper link in your popup content is trivial:
var marker = L.marker(latLng).bindPopup(
"<strong>" + props.name + "</strong><br />" +
props.address + "<br />" +
(props.description ? props.description : "") +
"GPS: " + feature.geometry.coordinates + "<br /><br />" +
"<a href='http://map.project-osrm.org/?z=18¢er=" + lat + "%2C" + lng +
"&loc=" + lat + "%2C" + lng +
"&loc=" + lat + "%2C" + lng +
"&hl=en&ly=&alt=&df=&srv=' target='_blank'>Get directions from this point</a>");
Updated demo: http://jsfiddle.net/ve2huzxw/110/
Or using L.Util.template
:
var marker = L.marker(latLng).bindPopup(
"<strong>" + props.name + "</strong><br />" +
props.address + "<br />" +
(props.description ? props.description : "") +
"GPS: " + feature.geometry.coordinates + "<br /><br />" +
L.Util.template("<a href='http://map.project-osrm.org/?z=18¢er={lat}%2C{lng}&loc={lat}%2C{lng}&loc={lat}%2C{lng}&hl=en&ly=&alt=&df=&srv=' target='_blank'>Get directions from this point</a>", {
lat: lat,
lng: lng
}));
Updated demo: http://jsfiddle.net/ve2huzxw/111/
EDIT 2:
If I understand correctly, you bind the popup in your interaction
function. So this is where the OSRM link should be added.
The coordinates are available in feature.geometry.coordinates
as an array where item 0 is the longitude and item 1 is the latitude (as required by GeoJSON spec).
function interaction(feature, layer) {
var props = feature.properties,
coords = feature.geometry.coordinates;
var popupContent = "<strong>" +
props.name + "</strong><br />" + props.address + "<br />";
if (props.description) {
popupContent += props.description;
}
// You already access the coordinates, what is the extra difficulty to use them in the OSRM link?
popupContent += 'GPS: ' + feature.geometry.coordinates + "<br /><br />" +
L.Util.template("<a href='http://map.project-osrm.org/?z=18¢er={lat}%2C{lng}&loc={lat}%2C{lng}&loc={lat}%2C{lng}&hl=en&ly=&alt=&df=&srv=' target='_blank'>Get directions from this point</a>", {
lat: coords[1],
lng: coords[0]
});
layer.bindPopup(popupContent, { // You can bind the popup right away, and it will automatically open when user clicks on the marker icon.
maxWidth: 320
});
// In your initial code, you bind the popup only after first click on marker, so you need 2 clicks to open the popup!
};
Updated Plunker: http://plnkr.co/edit/McR01O2u8eE7gPZ8qXpI?p=info
Notes:
- In your initial code, you bind the popup on layer click, i.e. the user needs to click once on the marker icon to trigger the binding, and to click a second time to open the popup… Why not binding the popup right away? It will be ready to be opened on user first click.
- You should review how you store your data (in your mapdata file) and how you convert it into overlays. This will save you from dozens of code copy-paste and improve the maintainability of your website.
- Do not forget to properly credit OpenStreetMap in the corner of your map!
Get Direction' but this is not correct of course – wocmultimedia Jan 04 '16 at 16:12
(Directions)'; – wocmultimedia Jan 06 '16 at 11:05