I would like to get the users location via geolocation and open google maps in a new tab with the route to my store.
I let the user click a button which starts the geolocation request and opens google maps with the coordinates as the source and my address as destination. I use the window.open function to open the new tab, but chrome and firefox see this as a popup. After searching a few post I found out popups are only allowed if they are directly in the onclick function. I cannot do this as I need the geolocation first.
Is there another way for me to do this?
This is the code I use:
$(document).ready(function () {
$("#btn-get-geo").bind("click", getLatLongFromGeolocation);
});
/**
* Get the location by accessing geolocation.
*/
function getLatLongFromGeolocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
routeInGmaps(position.coords.latitude + ", " + position.coords.longitude);
}, function (error) { }, { enableHighAccuracy: true });
}
else {
//Geolocation is not suported by your browser.
alert("No geolocation for you");
}
}
/**
* Open google maps with a route from the selected location to the store address.
*/
function routeInGmaps(from) {
window.open("https://maps.google.be/maps?saddr=" + from + "&daddr=" + storeLocation);
}