Living life one line of code at a time – by Andy Gup

Posts Tagged ‘accuracy’

No, using PhoneGap or Cordova doesnāt change how you retrieve geolocation information from within a mobile application and it doesnāt improve accuracy. You still use the W3C HTML Geolocation API coding pattern.

The reason why is because PhoneGap runs the HTML/CSS/JS code in a native OS WebView. ThisĀ is a chrome-less browser with mostly similar functionality as Chrome (Android) and Safari (iOS). There isn’t anyĀ additional functionality that would improve accuracy.

Some people have asked me if cordova-plugin-geolocation provides additional functionality. That plugin only works for devices that don’t already support geolocation and then it abides by the W3C Geolocation API spec.

The HTML Geolocation API in the browser also works offline. Yes, it’s true. You can use a web browser offline to get geolocation and even view a map without PhoneGap, here’s an example project that demonstrates the functionality in a number of ways. As long as the GPS on the device is enabled and working, and the user opt’s in at the geolocation-approval prompt then the application will receive GPS location data.

Are there any advantages to using PhoneGap for geolocation?

Yes, there are several potential advantages to using PhoneGap for geolocation. You can retrieve geolocation information while the device screen is locked and/or when the application is running in the background. You cannot do this with a typical webĀ app. As soon as aĀ browser is minimized aĀ web app will stop running even if the GPS is still running.

Another advantage is you can write a PhoneGap plug-in to tap directly into the native OS location API. Taking this approach will giveĀ you a greater level of control over the information you are retrieving. And, you have coding tools and access to APIs to better manageĀ the deviceās battery life in a way that the HTML Geolocation API cannot deliver. In the case of Android you can tap into significantly greater information such as details onĀ GPS satellites, granular information onĀ device’s internet connection,Ā as well as NMEA strings and more.

The Geolocation API is built into all modern mobile browsers and it lets you take either a quick, onetime snapshot, or you can get continuous location updates. Using the browser to get your approximate location is very, very cool, but itās also fraught with many challenges. The vast majority of blog posts on this API talk about what it can do, this blog post focuses on how to best use it and understanding the data provided by theĀ API.

To start things out, letās take a quick look at a short list of some of the challenges when using the Geolocation API.

Challenge 1. You will not know where the location information is coming from. Thereās no way to tell if itās from the GPS, the cellular provider or the browser vendors location service. If you care about these things then the native Android SDK, for example, gives you a huge amount of control over what they call ālocation providers.ā

Challenge 2. You cannot force the app to stay open. This means that the typical user has to keep tapping the app to keep it alive otherwise the screen will go to sleep and minimize your app.

Challenge 3. Speaking about minimizing apps, when the browser is minimized the geolocation capabilities stop working. If you have a requirement for the app to keep working in the background then youāll need to go native.

Challenge 4. Youāll have very limited control over battery usage. Second only to the screen on your phone or tablet, the current generation of GPS chips are major energy hogs and can suck down your battery very quickly. Since the Geolocation API gives you very little control over how it works you cannot build much efficiency into your apps.

Challenge 5. Most smartphones and tablets use a consumer-grade GPS chip and antenna, and that limits the potential accuracy and precision. On average, the best possible accuracy is typically between 3 and 10 meters, or 10 ā 33 feet. This is based on my own extensive experience buildingĀ GPS-based mobile apps and working with many customers who are also using mobile apps. Under the most ideal scenario, the device will be kept stationary in one location until the desired accuracy number is achieved.

Whatās it good for? Okay, you may be wondering what is browser-based geolocation good for? Itās perfect for very simple use cases that donāt require much accuracy. If you need to map manhole covers, or parking spaces, or any other physical things that are close together youāll need a GPS device with professional-level capabilities.

Here are a few generic examples that I think are ideal for HTML5 Geolocation:

Simply getting your approximate location in latitude/longitude and converting it to a physical address.

Finding an approximate starting location for searching nearby places or things in a database or for getting one-time driving directions.

Determining which zip code, city or State you are in to enable specific features in the app.

Getting the approximate location of a decently sized geological feature such as a park, a building, a pond, a parking lot, a driveway, a group of trees, an intersection, etc.

Whatās the best way to get a single location? The best way to get a single location is to not use getCurrentPosition() but to use watchPosition() and analyze the data for a minimum set of acceptable values.

Why? Because getCurrentPosition() simply forces the browser to barf up the best available raw, location snapshot right now. It literally forces a location out of the phone. Accuracy values can be wildly inaccurate, especially if the GPS hasnāt warmed up, or if you arenāt near a WiFi with your WiFi turned on, or if your cellular provider canāt get a good triangulation fix on your phone, or it returns a cached value from a different location altogether. There are many, many āwhat ifs?ā

So, I recommend using watchPosition() and firing it off and letting it run until the return values meet some minimum criteria that you set. You need to know that while this is happening the location values returned may cover a fairly wide geographic areaā¦remember our best accuracy values are 10 ā 30 meters. Hereās a real-world example of Geolocation API location values that I captured over a 5 minute period while standing stationary in front of a building.

What steps do you recommend? Here are fiveĀ basic steps to help guide you towards one approach for getting the best location. This is a very simplistic approach and may not be appropriate for all use cases, but I think itās adequate to demonstrate the basic conceptsĀ for working towards determining the best possible location.

Step 1. Immediately reject any values that have accuracy above a certain threshold that you determine. For example, letās say weāll reject any values with an accuracy reading greater than 50 meters.

Step 2. Create three arrays, one for accuracy, latitude and longitude. If the accuracy is below your threshold, or in this case < 50 meters, then push the values to the appropriate arrays. You will also need to set a maximize size for the array and create a simple algorithm for adding new values and removing old ones.

The array length could be 10, 20 or even 100 or more entries. Just keep in mind that the longer the array, the longer it will take to fill up and the longer the user will have to wait for the end result.

Step 3. Start calculating the average values for accuracy, latitude and longitude.

Step 5. If your arrays fill up to the desired length and the average accuracy meets your best-possible criteria, and the standard deviation is acceptable then you can take the average latitude, longitude values as your approximate location.

When I presented at OSCONĀ (OāReillyās Open Source Conference) in Portland, Oregon this last week on native Android GPS and Geolocation, I was repeatedly asked the question āis the GPS in smartphones good enough?ā In general the answer is āyesā. But, I should back this statement up by looking at several everyday types of scenarios to help illustrate my answer.

What does accuracy mean? First lets briefly look at what accuracy means.Ā Accuracy, in a technical sense, means you get a latitude, longitude and accuracy number from the GPS. Then you can draw a circle using the accuracy number as radius around the latitude/longitude point. It’s highly likely that your actual location is somewhere within that circle. Itās unfortunate that consumer GPS devices donāt also come with a probability factor that would indicate how much to trust the accuracy number. As it is, we will have to take it on a certain amount of faith that our real location is, in fact, somewhere within the accuracy circle.

Under absolutely perfect conditions your typical smartphone GPS chip will deliver around 3 meters (~10 ft) in accuracy for several minutes at a time.Ā Standing on the highest mountaintop in the Rocky Mountains with no clouds in the sky might come close to being a perfect condition. Under what Iāll call āaverageā conditions, which reflect everyday in-city usage patterns, you can expect accuracy from 3 ā 150 meters (10 ft – 500 ft) or greater and the accuracy number typically fluctuates quite a bit over a period of minutes or hours. GPS signals are affected by anything that interferes with your smartphone receiving the weak transmissions from GPS satellites circling above us. Nearby trees, cars, buildings, big weather storms can all reduce accuracy, and so can being inside a building or underneath trees.

Getting the weather.Ā Now back to the everyday scenarios I mentioned. I bet that most people who own smartphones use them to check the weather at least once per day. Furthermore, weather is typically affecting a large geographic area so the vast majority of forecasts cover cities, counties, States, regions or even entire countries.

Iām going to argue that for this scenario a GPS accuracy of 1000 to 2000 meters, or 0.6 to 1.24 miles is good enough to get started with a finding places app. Values in this accuracy range can be easily and quickly retrieved by a typical smartphone.

Finding places around me. Almost everyone that owns a smartphone has used an app to search for food, gas, groceries etc that are nearby. Ā Thereās no hard written rule, but I think most people would agree that users who are looking for places around them tend to be less concerned about high levels of accuracy. Some applications let you choose target levels of accuracy such as 1 mile, 10 miles or even up to 100 miles for the search radius.

Iām going to argue that for this scenario, as well, that a GPS accuracy of 1000 to 2000 meters will also work perfectly fine.

Real-time driving directions. There are only a handful of applications that do this on smartphones, and even fewer apps do it really well. Thereās a lot of hidden math involved in making everything look smooth to the user. These apps wipe away all of the complexity: simply give it a starting point and an endpoint and then away you go.Ā Weāve all used these types of applications so we know they work well the majority of the time even with the occasional navigational glitch, hardware lockup or low battery.

Social Media location. Hundreds of millions of people use location-aware social media apps every day. Some of these apps provide you with discounts, give-aways and coupons for retail locations that are around you.Ā For a typical big box store, itās easy for an app to place you in the parking lot of a Target, Walmart, or a large supermarket and promotions can be based on your location, time of day or day of the week. Itās more challenging for stores with smaller storefronts to use targeted advertising unless someone is simply āin the vicinityā.Ā For example, tiny stores, kiosks and shops trying to compete in high traffic tourist areas have to compete with many other vendors. In a crowded marketplace area, even with 3 meter accuracy you could be standing next to four or more different storefronts.

A final few words

Hopefully these short examples have successfully illustrated the point that for typical consumer-focused applications smartphone GPS is simply good enough. Certainly there are many, many more scenarios that could be examined so I tried to pick the most common ones. Because of the lower accuracy requirements you can get less accurate results faster from a GPS. Speedy results can mean everything for todayās consumers who have high levels of intolerance for application delays.

The opposite is also true, the greater the accuracy requirements the longer it can take to get a more precise GPS measurement. As Iāve mentioned in my others posts on this subject, itās takes time for a GPS device to get a fix and then it will try its best to hold onto it as you move around. I suspect that most consumers are significantly less demanding about accuracy as compared to commercial and government users. If consumers were more demanding then there would be a greater uproar about GPS accuracy.

To better understand how to make the most of location data check out the other posts Iāve written in the reference section below.

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.

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:

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.

This post summarizes the six most common use cases associated with using GPS and location services on an Android device. It also continues the series on using the open sourceĀ GPSTester tool to build better location aware Android apps. So not only can you read about what works and what doesn’t you can also try various scenarios out yourself using the tool rather than having to build code from scratch.

Iām striving to make this information freely available because adding location to your apps can be fun, and it can also be an important part of the applications that you build. By taking into account these six use cases you will be able to build applications that better meet your requirements and make for a more enjoyable end user experience.

Use Case 1 – Cold Start. Application launches from a completely stopped state. Cached GPS Ā and Network location values may be unreliable. As a developer you donāt really have any control over this but you need to plan for it. The screenshot from the GPSTester tool below shows a typical cold start where the cached network location has better accuracy than the most recent GPS location. You can see from the timestamps that even though the GPS location is more recent than the network location, it’s the network location that has better accuracy.

Use Case 2 – Warm Start. Application launches from a minimized state. Cached GPS values may be reliable depending on how much time has passed since application was last running, and the total distance traveled away from when the app was last used. Like the cold start, the user that decides how and when to start the app and you just have to plan for this use case. The screenshot below represents a typical warm start scenario where the cached GPS result has better accuracy than the cached network provider.

Use Case 3 – Minimized. Requirements may call for location listeners to continue to run in the background, or to be shut off when the application is minimized. There is also the option of using what’s called passive listeners which rely on other applications to call location services. Note, if you listen for location changes in Ā a minimized state you typically won’t benefit from this information until the application is opened again. In my previous posts I’ve stated that if your target demographic uses retail and social media apps this use case may be of benefit if the person uses the device constantly during the day. Many retail and social media apps access location. However, in many commercial use cases using passive location listeners may provide less than desirable results since there is much less of a chance of another application using a location service.

Pros:Ā Running location services while app is minimized can speed up acquisition time when app starts, but only if your requirements call for always-on accuracy at a momentās notice. If you shut off location then you will save battery life.

Cons: If active location services are left on in a minimized state this can sap the battery without the user really knowing it.

Use Case 4 – Snapshot. Application only needs to hit a minimum level of accuracy before shutting off location services. The screenshot below shows that it took approximately 2 minutes for the device to reach an accuracy of 12 meters. Yep, you heard that right…2 minutes! Also note how much the latitude and longitude of the different providers wandered around the map. The blue dot indicates network locations and the red dot indicates GPS locations. This is just a reminder that consumer-grade Android devices may or may not be accurate enough for your unique requirements.

Pros: Maximizes battery life.

Cons: May cause user interface delays if user needs to refresh the location during a single application session.

Use Case 5 – Continuous duty. Location services need to be constantly on while application is running. The screenshot below shows a use case of the user being in a downtown area with lots of tall buildings, the apps was run for about 10 minutes, then minimized briefly, then opened again for a warm start. The accuracy didn’t change much even after 10 more minutes of testing. As you can see, the GPS provider accuracy has suffered significantly at 153 meters, while the network provider is offering 45 meters of accuracy.

Pros: Best for constant, up-to-date, always on accuracy. Accuracy available instantly (after the device has ‘warmed up’).

Cons: Huge drain on the battery, but you can adjust the minimum distance property needed to trigger a location update.

Use Case 6 – Intermittent duty. GPS is only needed to run at intervals. To test these types of scenarios you can adjust various settings from within the GPSTester Tool’s preferences. Specifically you can modify the GPS and Network properties for minimum update time and distance. This is very powerful as you can easily toggle these settings to test various settings on-the-fly versus have to write custom code.

Pros:Ā Depending on your use cases such as delivery driver tracking application, this can provide a good compromise on battery usage and accuracy.

Cons: You will have to write algorithms to constantly adjust the location service settings to meet the users movement patterns. This may also involve cycling the location services on and off to maximize battery life.

A few comments on testing your use cases. In the GPSTester tool there are a number of settings you can use to adjust how the device will receive location information.

You have full control over which providers are being used whether itās GPS, Network or Critera.

Location provider indicators on the main screen will show which providers are being used. You can also see which provider is providing the best accuracy as determined by comparing available providers.

There is a list of available providers available on the GPSTester tool as well as table row showing what the device considers to be providing the best accuracy.

If you are a developer building applications that require location information then you need to know what is really possible with the HTML5 Geolocation API and not a bunch of hype. The blog post attempts to give you some insight into how it works with desktop and mobile browsers as well as having a greater appreciation for what is and what isnāt possible. Iām going to show you that accuracy depends on many factors, some of which are beyond your control, and at best the location information returned by the API is just an approximation.

Most common use case. For the most part, HTML5 Geolocation works just fine in dense urban areas when you are stationary with your laptop or smartphone Wifi turned on. This is the use case most commonly cited when questions are asked about accuracy. This makes sense because urban areas have many public and private Wifi routers and cell phone towers are typically closer together. As you’ll see, HTML5 uses these and other methods to pinpoint your location. However, itās not always that simple and below are some other use cases that you should take into consideration. Ā

How does the API work?Ā Depending on which browser you are using, the HTML5 Geolocation API approximates location based on a number of factors including your public IP address, cell tower IDs, GPS information, a list of Wifi access points, signal strengths and MAC IDs (Wifi and/or Bluetooth). It then passes that information to a Location Service usually via an HTTPS request which attempts to correlate your location from a variety of databases that include Wifi access point locations both public and private, as well as Cell Tower and IP address locations. An approximate location is then returned to your code via a JavaScript callback.

As an example to show you what type of information is sent to a Location Service, I did some basic testing with Firefox 11. Firefox uses Googleās Location Service. On a related note, as far as I can tell with Firefox 11 it isnāt passing cookies any more where in Firefox 3.6 they use to pass a user ID token.

Firefox 11 browser sends queries to https://maps.googleapis.com/maps/api/browserlocation/json? The example results have been obfuscated, but by looking at it you should get the idea of what content is being sent:

Not all Geolocation services are the same, and they certainly donāt all use the same algorithms and exact same databases. Because of this the results typically vary across browsers that use different Geolocation services.

Hereās my best attempt to document which Geolocation service each of the major browsers are using. I havenāt done any definitive testing however I do know from experience that different browsers and even different laptops for smartphones will return different locations when tested from the exact same location. Some location services are better in some cities and others are better in other cities. I havenāt come across a definitive list, most likely because the information is constantly being updated. Iāve included a link to a demo application at the bottom of this blog where I encourage you to also test the API against different browsers.

Chrome uses Google Location Services.

Firefox on Windows uses Google Location Services.

Firefox on Linux uses GPSD –Ā http://catb.org/gpsd/. Iām not sure if this includes Android. I havenāt had a chance to test it yet.

Iām not sure what Safari on Windows uses. With all the public distrust between Apple and Google, I wouldnāt be surprised if Safari on Windows also uses Appleās Location Service, but I havenāt found any documentation to verify this and I havenāt tested it.

Opera uses Google Location Services. On a related note, Iāve also noticed that mobile Opera on Android accesses the GPS. This is something to consider from a battery usage standpoint.

Not all browsers support HTML5. Itās important to note that not all browsers support the HTML5 Geolocation API, for example Internet Explorer 8. The HTML5 Geolocation API is built into the browser and is accessible using JavaScript methods that access the navigator object. In order to work it requires HTML5 support in the browser. You can research whether or not a particular browser supports Geolocation by going here: http://mobilehtml5.org/ or http://caniuse.com.

Additionally, if a user has disabled JavaScript for some reason, then your Geolocation app wonāt work in their browser. JavaScript code is required to access the API.

HTML5 Geolocation requires an internet connection. If you lose your internet connection then you wonāt be able to access the Location Service. With no internet connection most browsers will not return a location. Sometimes you can access a cached location that is stored in the browser by the API. But, that cached location is the last valid location that was calculated by the API.

Is Wifi turned on or off? If Wifi is turned off on your phone, desktop machine, laptop or tablet , the Geolocation API service will try to find your location by other methods which include your public IP address, Cell tower ID triangulation or GPS. Public IP addresses databases usually return a location for your internet providers Point of Presence or PoP. Furthermore, some internet provides offer rotating IP addresses. So you get to use one IP address for a particular time period such as 48 hours and then you get a different one. So a Public IP address is usually only good enough to locate you to a particular City, or a general area of the City, or a Country depending on where you are in the world.

As for Cell Tower IDs it depends on what type of information your particular phone and Telco Carrier provides to the API. Some smartphones only return information on the current tower that the phone is pinging, which obviously makes triangulation very difficult and decreases accuracy to within a radius around that tower.

Iāve noticed that the native Android browser is significantly less accurate without Wifi. Without it I typically see accuracy numbers in the 1000+ meters range. As soon as I turn Wifi back on and Iām in a neighborhood or downtown area, the accuracy drops to less than 75 meters almost instantly.

Are they in a rural or urban location? Granted the vast majority of users will be in urban locations. However if you have requirements for users traveling outside of urban areas then this section applies to you. Geolocation in rural areas is significantly less reliable. If Wifi is turned on but the user is not near any Wifi access points, then the Geolocation service will also attempt to fallback to the other methods mentioned above. Ā Triangulation can be much more difficult in rural areas where towers are spread further apart, and for browsers that donāt use GPS the accuracy will suffer significantly.

Are you moving or stationary? Being stationary in an urban area offers far better accuracy with the Geolocation API than when you are moving. On my native Android phones itās rare to get an accurate reading while driving around town. Occasionally a sporadic result would be returned when you stop at a light. To date, I have never gotten a valid reading while driving on a highway at speeds over 50 mph.

Is a VPN turned on? If a VPN is turned on, then the location will resolve to the VPNās public IP address. For example, a user in Denver is logged into the company VPN which host is hosted at their headquarters office in a suburb of Dallas, Texas. The HTML5 Geolocation API will resolve the location to the headquarters public IP address in Dallas and not the userās actual location. Quite a few corporate users have VPNs for security reasons.

Custom Geolocation as a fallback? Depending on your requirements you may want to implement your own IP Geolocation using a company such as IP2Location. Or use a third-party Geolocation service, such as Skyhook, as a fallback. Remember IP Geolocation only returns locations to a City or an area within a City. So, if you need more accuracy than that for your application, then donāt bother with this approach.

The downside to custom IP Geolocation is that this requires writing a server-side service to grab the browsers IP address. All server-side languages such as PHP, C#.NET, Java and JSP support these capabilities. You also have to subscribe to another service that lets you query their database by IP address and get a return value of an approximate location. There is no current way to get this information from the browser, on the client-side, using JavaScript.

HTML5 Geolocation doesnāt meet my requirements, what do I do? If you have critical requirements for gathering more precise location information than the HTML5 Geolocation API is capable of delivering then Iād recommend building your application using a native API such as Android or iOS.

How can I test this? You can test HTML5 Geolocation in different browsers using a test application that I built. I recommend trying it on different browsers and comparing the results yourself: