Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining the location of mobile web site user

Is there anyway to track the location (as precise as possible) of a user of a mobile web site?

I am aware that the best way to locate someone is via GPS, but I'm not sure if that is possible through a mobile web site? I also know that you can roughly track a users location via the device's IP address, but I'm not sure if this is the best method?

Looking at Google Maps as an example, the Google Maps website can track my current location pretty much precisely using an iPhone or an Android device. Is this because these sites are activating the device's GPS capabilities, or is there something else going on in order for them to achieve this?

The website I am planning to create will ideally be capable of running on Smartphone devices (such as Android/iPhone/Blackberry/Windows Phone etc), but also be capable of running on non-smartphone devices, which may not have GPS technology built in to query the current location.

Can anyone suggest the best way this could be achieved please? I know that some existing location libraries such as GeoLocation are widely recommended, but again, are these compatible with devices which don't necessarily have GPS technology available?

Thanks in advance.

like image 342
jkulisic Avatar asked Jan 17 '23 10:01

jkulisic


1 Answers

You can use the HTML5 Geolocation API on devices that support it. It will return the most accurate location that is available which might be A-GPS or wifi location.

See for example this tutorial on how to use geolocation with JavaScript.

Basically you test for geolocation support and request the position:

if (navigator.geolocation) { // device can return its location
    navigator.geolocation.getCurrentPosition(function(position) {
         console.log(position.coords.latitude);
         console.log(position.coords.longitude);
    });
}

Please note, that this code will prompt the user if they want to share their current location or not.

like image 123
Oiva Eskola Avatar answered Jan 30 '23 10:01

Oiva Eskola