2

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);
}
Jerodev
  • 32,252
  • 11
  • 87
  • 108
  • window.open will open a window or tab, depending on the user settings. I don't think you can do anything about that. – html_programmer Jan 17 '14 at 15:40
  • The issue of opening a tab with window.open has been answered in the previous posting: http://stackoverflow.com/questions/4907843/open-url-in-new-tab-using-javascript – Andrew - OpenGeoCode Jan 17 '14 at 19:32

0 Answers0