Where Part 1 focused on non-GPS enabled devices, Part 2 is totally focused on mobile web geolocation. The great news is that the usage of HTML5 location services along-side the fact that there is a GPS chipset in most, if not all, modern smartphones and tablets dramatically improves the chances of getting an accurate location. And, besides that fact — mobile geolocation is simply a lot of fun to work with.
I also want to point out that there are an increasing number of really good blog posts covering the topic of â€śhow to use” the API that look at the nitty-gritty of how the code works. This post is different in that Iâ€™ve tried to focus on “how to build successful applications” with the API, and how to get the most out of the API so that you can successfully implement your unique requirements.
Whatâ€™s different about desktop vs. mobile HTML5 Geolocation? With mobile you can access the GPS if itâ€™s available. Itâ€™s important to note that in order to access a device GPS you have to set the optional enableHighAccuracy property in your code. Contrary to what is shown in some samples on the internet, you can use this property with both theÂ getCurrentPosition() and watchPosition() functions.
//One time snapshot
// Optional settings below
//Tracking users position
watchId = navigator.geolocation.watchPosition(
// Optional settings below
How accurate is it??? This is the million dollar question, right?Â When using enableHighAccuracy() on a phone where all the appropriate permissions have been selected and granted, Iâ€™ve typically seen accuracy readings as low as 3 meters (~10 feet) that were obtained within 10 â€“ 30 seconds of kicking off the geolocation functionality. I’d consider that excellent for most consumer and retail applications. You should be aware that like any location-based functionality you will get spurious (abnormal) results that fall way outside the norm, and sometimes these results are wildly wrong.
Iâ€™ve seen claims that using the enableHighAccuracy()Â property slows down the phones ability to deliver a location. Iâ€™m going to argue that those claims are misleading. It is true that the GPS, itself, can take a significant amount of time to warm up and start delivering high accuracy results. For an in-depth look at that topic see my post on the Six Most Common Use Cases for Android GPS. However, there are conditions where simply enabling the enableHighAccuracy() property doesnâ€™t affect the speed in which you can get the initial result. More on these topics below.
What is the best way to try out various configuration scenarios? Iâ€™ve built an HTML5 Geolocation Testing tool that can be used in browser, or it can be repurposed to work in PhoneGap or Titanium. It is a jQuery-based mobile application that includes a map and settings view were you can adjust all the different properties and try out different configuration scenarios. It’s a work-in-progress so I welcome suggestions and pull requests.
Â Why HTML5 Geolocation rather than native? Applications using HTML5 Geolocation typically have slightly different requirements than native GPS-based applications. Each platform has its advantages and disadvantages and it all comes down to your requirements, budget, timeframes and skill sets:
- Donâ€™t have access to native platform developers or skillsets on Android, iPhone and/or Windows Phone.
- Need a cross-platform stand-alone web app, or a web app that has been repurposed to work with PhoneGap or Titanium.
- Quickly locate the user/consumer within a reasonable expectation of accuracy.
- Typically it is a non-commercial, consumer grade application that does not have extremely high accuracy requirements (e.g. < 1 meter).
How fast can I get an initial location result? The answer is very fast, potentially within a few seconds, given the following scenarios:
- If there was a cached GPS or Network location stored on the phone. The GPS location is, of course, from the GPS chipset. The Network location comes from your wireless carrier and is dependent on your phone and their capabilities.
- How the timeout and maximumAge properties are set. If you set timeout = 0 and maximumAge = Infinity it will force the application to grab any cached location, if one is available. Other settings may result in delays.
- If the phone or tablet has decent internet connectivity and Wifi enabled.
- If the device is in an urban area with many wifi nodes broadcasting their SSIDs nearby.
- The device has a clear and uninterrupted view of the sky. GPS’s listen for a very weak signal from multiple satellites. These signals can be partially or completely blocked by buildings, thickÂ foliage, vehicle roofs, etc.
Â How accurate is the initial location result? Hah, you might have guessed Iâ€™d say that it depends. Â When you first kick off a geolocation request, accuracy does depend on a number of different factors that are mentioned above. And itâ€™s safe to say that, in the vast majority of cases, the first location is not the most accurate and typically not the most dependable. If you want the fastest, most accurate location possible then you will most likely need to either do multiple snapshots, or use watchLocation until your desired level of accuracy is met. It’s important to note because I’ve been asked about this many times, you cannot expect the GPS, itself, to have enough time to lock onto a satellite and deliver a fast, accurate initial location. It may take dozens of seconds or even minutes. Yep, it’s true. Factors that affect initial location accuracy include:
- Cached locations – how recently the user accessed location functionality. For example, applications like Facebook typically grab a location when you open the app. So frequent users of social media are more likely to have a fresh, cached location that non-social media users. If you are targeting business travelers, the cached location might the last city before they got on a plane. Or, it could be your home neighborhood and not where you work or go to games.
- Wifi turned â€śonâ€ť. If the Wifi is turned on then the device can access the location service and there is a much greater chance that the initial result is fairly accurate. If you didn’t have a chance to read Part 1, when the Wifi is on your browser gathers local Wifi node information from your Wifi card, and it can use that information in a location service provider request over the internet to try and triangulate your position. Typically this means your initial location can be within a block or two of the actual position. Also, it is possible if Wifi is turned on that you can get a significantly more accurate initial location than if you were using GPS by itself with no Wifi or internet.
- Internet connectivity strength. If you have a poor internet connection and no Wifi, then the browser’s requests to the location service can be delayed, blocked or even interrupted.
- No VPN. Take note commercial application developers: as mentioned in Part 1, if VPN software is in use it can wildly affect accuracy and even place you in another State (or Country).
Can I use HTML5 Geolocation for mobile tracking? Yes, with caveats. Typically HTML5 tracking applications are built inside a native wrapper framework such as PhoneGap or Titanium. There are several immediate problems with stand-alone, browser-only HTML5 tracking applications. First, there is no built-in functionality to keep the screen from going to sleep. Second, when the screen goes to sleep the HTML5 Geolocation functionality also goes to sleep. Native-based tracking applications can work around these limitations and listen passively in the background when they are minimized. Third, you have little control over the GPS settings to help management battery consumption.
Can I use HTML5 Geolocation offline? Yes! If there is no cellular connection or Wifi available, then HTML5 Geolocation can still access cached locations and real-time GPS information. This is vastly different from what was discussed in Part 1 as related to applications targeted at laptops, desktops and tablets that may or may not have GPS. If a device does not have a built-in or externally available GPS then your offline application will not work.
Handling abnormal location results. Your application will occasionally encounter widely inaccurate results and you need to handle these gracefully for the best user experience possible. My recommendation is to check the timestamps and distance traveled between the current geolocation object and the previous one. If the distance or speed seems excessive then youâ€™ll need to reject the result. In the reference section below is a link to more information on calculating the distance between two points containing latitude and longitude. As an example, see the attached screenshot with the spurious results indicated by red circles. Also note in the screenshot the accuracy level was 3 meters, so it’s important to understand that even at high accuracy levels you still need to very that each location meets your minimum requirements. This way your results will always look polished and professional to the end user.
What are some of the downsides of using HTML5 Geolocation versus native? The bottom line is that for simple location gathering and basic tracking HTML5 Geolocation is just fine. This should meet the requirements for most consumer applications. For anything more complex than that you should consider looking at going native.
- It may not work on older phones and older browsers (depending on your definition of old). See below in the references section for a link to a fallback library to handle these situations.
- HMTL5 Geolocation offers significantly less control over GPS settings. This can have an unacceptable impact on more complex applications. Â Because of this, I also suggest that HTML5 Geolocation is not suitable for long-running tracking applications.
- Battery life management. This is a direct result of bullet #2. Itâ€™s more challenging to manage battery life with HTML5 Geolocatoin if your requirements call for continuous use of the GPS. Â Your control is very limited with respect to these two properties: timeout and maximumAge.
- Cannot use it when the application is minimized. If your requirements calls for the ability to passively receive locations while in a minimized state then, as mentioned earlier, you will have to go native.
- Very little control over how often you want location updates. You’ll need to do a bunch of custom coding to emulate what is already built into native application APIs. For example, the native Android API offers very detailed control over what type of geolocation data you can get access to, how you can access it and how often. Read more on that topic in my post on How Accurate is Android GPS Part 1 â€“ Understanding Location DataÂ and also take a look at Android’s LocationManager Class.
W3C Geolocation API SpecificationÂ
HTML5 Geolocation Test Tool
Mozilla â€“ Using Geolocation
Calculating distance between two points.
Geolocation fallback library for older browsers