Part 7: Add GPS Reading

Ok, so now we have created a PhoneGap compass application with an arrow that always points to the North. Next, we’ll also add to this the latitude and longitude readings of our current position. For this we’ll need to add to the project the geolocation plugin

cordova plugin add org.apache.cordova.geolocation

to which the response should be (assuming you’ve only created an android project):

The next part is to add the JavaScript code that reads the current coordinates from the GPS sensor on the smartphone.

Temporarily, just to see that the application is indeed trying to get the GPS coordinates, we’ll change in the config.xml file from:

<preference name="fullscreen" value="true" />

to

<preference name="fullscreen" value="false" />

This will enable us to see on Android the device status bar and verify that the application is indeed trying to get a fix on it’s location via the GPS (you should ensure that the GPS is indeed turned on)

The API for the geolocation plugin can be viewed in the Cordova plugin reference documentation, but the code below (which should replace our previous index.html) should be self explanatory: