Parse Gmap Url
by Arjan Haverkamp
JavaScript
function parseGmapUrl(url) {
const UrlRegex = /^https?:\/\/(?:www\.)?google\.com\/maps\/dir\/((?:(?:[0-9]+\.[0-9]+),(?:[0-9]+\.[0-9]+)\/?)+)(?:\/[^\/]+)?\/data=(.+)/;
const match = url.match(UrlRegex);
if(match === null) {
alert('Invalid URL!');
return;
}
const coords = match[1].split('/').map(c => c.split(',').map(parseFloat));
const travelMode = [
"DRIVING",
"BICYCLING",
"WALKING",
"TRANSIT",
][match[2].slice(-1)];
return { coords, travelMode }
}
console.log(parseGmapUrl('https://www.google.com/maps/dir/52.1832814,5.4325584/52.3169013,5.8843473/@52.1655446,5.5692265,9z/data=!4m25!4m24!1m21!3m4!1m2!1d5.7812337!2d52.0802218!3s0x47c7b18673f69d01:0x2de8f3dfecc91934!3m4!1m2!1d6.1027005!2d52.0609285!3s0x47c797db3e5db333:0x8dcd84c6bc12a5c7!3m4!1m2!1d6.2197343!2d52.0972828!3s0x47c7914559f8af13:0x2a02df131cc2a415!3m4!1m2!1d6.302645!2d52.1729452!3s0x47c792c019a63be5:0xe1a5b57399dcb929!4e1!1m0!3e0'));