Search code examples
javascriptgoogle-mapsgeolocationdriving-directions

Geolocation on smartphone devices doesn't run


in my website I have to launch google maps for driving directions when I click on a button. I know the end point but I must communicate the starting point, that's my position, with geolocation. I've a problem with geolocation on smartphone devices. This is my code:

jQuery(window).load(function(){ if (navigator.geolocation) navigator.geolocation.getCurrentPosition(function(position){ myLat=position.coords.latitude; myLng=position.coords.longitude; jQuery(".btn").attr("href", "http://www.google.it/maps?saddr="+myLat+","+myLng+"&daddr=44.000000,7.000000")}; }); });

It works well on pc, but on smartphone it doesn't run. An error occures. Should I write a different href for these devices? If so, how?? Thanks a lot for your help!!


Solution

  • I think I solved it. I detect if I'm using mobile devices and for them I use this code

    if( isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows()){
            jQuery(".btn").attr("href", "https://maps.google.com?saddr=Current+Location&daddr=44.000000,7.000000")
        }
    

    Thank you for your support Riccardo