I have a Lenovo Miix 2 tablet and I am developing a project with a Java servlet. I want to read GPS data in my project. Can I read this data using Java? and/or HTML5?

In this project, I can't use the Internet to obtain the position of the user. And this is a local web application and the user views the application in a browser.

  • Is this really specific to GIS? It looks to me like it would be better suited to StackOverflow or possibly SuperUser, since you're just wanting to pull in GPS coordinates from hardware to a java/html5 application.
    – Chris W
    Feb 22, 2015 at 20:44
  • Because my Application was in GIS scope's, I Asked it here! Feb 24, 2015 at 12:08

2 Answers 2


(This answer addresses the comments of John Barça’s comments… a little too large to put into the comments section).

Again, I got this to work using PhoneGap, but I should perhaps clarify things a little bit.

1) I used PhoneGap Build instead of installing PhoneGap on my computer (glad to have a workmate who can give me this great kind of advice!). You will not have to deal with the installation and other things, and you can just zip your entire web app and upload it to Build, which then converts it to files for either Android, iOS or Windows. You are limited to 50 MB and you can only upload one app at a time (if you are using the free version, that is; if you are willing to pay, then you have more possibilities). It works very well. You have to seriously test your app, of course, and see if some bugs might have been introduced, but if there were, then you can use remote debugging to look into what is going wrong... once you figure out how to get that set up you can debug just like using the Chrome Developer Tools, or Firebug. As a side note, my app was built using jQuery Mobile, OpenLayers, geolocation, and even offline tiles (created in TileMill, using OSM Bright, so yes, the app can be way more than just a very simple HTML file that uses some JavaScript somewhere).

2) I did not use the geolocation functions from PhoneGap but the Geolocation API in general. If you look at the details then you realize that the former is built on the latter anyways, and even the syntax looks quite similar. But generally, going the “normal” route without even looking into the PhoneGap docs works fine. If in the options object you set enableHighAccuracy to true then it will always look for the best possible way to locate the device, which, if available, is GPS. So make sure that setting is set to true (look at the link I provided and navigate to Fine tuning response). Otherwise it might look for quicker but less accurate ways to locate you (e.g., IP address, wifi triangulation, etc.), which will not work either way if you are using your app offline.

3) (this point is more of a heads up, but not entirely linked to the response) As the app should work offline you also have to find a way to get to display an offline map. If you are using PhoneGap Build then it is important that you keep the 50 MB limit in mind! Tilesets can get very large very quickly (especially if you enable the zoom levels that show the most detail)! If your area is quite small then that would be of no concern you, but if you are looking at providing tiles for anything bigger than a city, then you would almost surely hit the limit. Your options then could be to let the user cache a map when online, and use it when offline (such as the Google Maps app lets you do it), or you could add some code to your app that pulls the tiles from a specific folder on your device, and then after the app has been installed you would have to drop your tiles in that exact folder, so they can be retrieved. You could do that using PhoneGap’s File API. I have not implemented either of those options yet, but my workmate and I will be looking into them soon, as it would be awesome to benefit from the simplicity of using PhoneGap Build, but still be able to load huge tilesets.


If you wish to use HTML5, you are probably better off using PhoneGap. PhoneGap abstracts away the significant differences between the hardware and APIs on different mobile platforms, iOS, Android, Windows, etc, allowing you to use Javascript, CSS and HTML to access the sensors, such as GPS. See the supported features/sensors grid. You cannot otherwise access the hardware resources on a mobile (or indeed any other) computing device directly with Javascript -- for obvious security reasons.

Using something like Phonegap, also has the advantage of being more or less portable to other mobile OSes.

I don't believe that you can access the Windows Sensors API directly with Java, but you can do so using JNI, see this presentation. It is likely to be much smoother using C# (or other .NET language) to access Windows specific devices and APIs, though this would reduce portability, if that matters to you.

  • In phoneGap you need to connect to internet, but I need collect my position in where the is no Access to Internet Feb 22, 2015 at 12:30
  • In future, could you please put all the relevant details (lack of internet connection being quite relevant) in your question, so people don't waste time answering based on insufficient information. Feb 22, 2015 at 12:47
  • sorry sir ....... Feb 22, 2015 at 12:51
  • No worries. Are you sure that you can't get GPS location without an internet connection. Sure, you will lose WiFi assistance, but you can still read the GPS directly. Feb 22, 2015 at 13:04
  • 1
    You do not need to connect to the internet to get the location with PhoneGap. It will look for the most accurate positioning method (unless otherwise specified), and in your case that would be GPS. I have developed a data collection app with PhoneGap and the most important thing about the app is that it works offline. Feb 22, 2015 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.