5

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.

jkulisic
  • 309
  • 2
  • 4
  • 13
  • you mean track the location of where the mobile phone is register? or the current location of the phone? (also I assume not using WIFI) – rcs20 Feb 22 '12 at 13:30
  • Ideally, where the user is currently located at the time of page access. The web request could happen through 3G or WiFi, but I'd imagine it'd be 3G primarily. – jkulisic Feb 22 '12 at 14:03

1 Answers1

10

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.

Oiva Eskola
  • 949
  • 8
  • 13
  • Thank you for the feedback. This API definitely looks good, however I am slightly concerned that it doesn't detect non-GPS based locations very well. Although understandable, I am wondering if whether this API running on a non-GPS phone would work better than a desktop machine (due to using cell location?) – jkulisic Feb 23 '12 at 13:02
  • I don't think there are any guarantees on the accuracy of the location. In general the phone will use whatever means possible to locate itself, while the IP address on desktop machines gives you a city level location at best. With the Geolocation API you can read the accuracy of the returned position with `position.coords.accuracy` – Oiva Eskola Feb 23 '12 at 14:14
  • Ahh okay, cool. Is there also a way of telling when a GPS device is not present? If that's the case, I probably just wouldn't show the "find my location" button on the page. – jkulisic Feb 23 '12 at 17:17
  • No, the only information you get beforehand is whether the phone supports the geolocation API or not (in which case you shouldn't show a "find my location" button). Even if it has support, the phone doesn't tell you how it got to know its position (GPS or other means), only the accuracy. There's lots of additional information here: [http://diveintohtml5.info/geolocation.html](http://diveintohtml5.info/geolocation.html) – Oiva Eskola Feb 23 '12 at 18:46
  • Thank you very much. You have been extremely helpful! – jkulisic Feb 24 '12 at 09:00