Book Image

PhoneGap By Example

Book Image

PhoneGap By Example

Overview of this book

Table of Contents (17 chapters)
PhoneGap By Example
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Detecting the current geolocation


It is very easy to detect a device's current location (latitude and longitude) with the Cordova org.apache.cordova.geolocation plugin.

Tip

Details of the Geolocation API are available on the W3C site at http://dev.w3.org/geo/api/spec-source.html.

This plugin detects the current location based on GPS, IP address, RFID, Wi-Fi and Bluetooth MAC addresses, and GSM/CDMA cell IDs. However, there is no guarantee that the location will be detected precisely enough.

Let's install it the same way we did for other plugins:

$ cordova plugin add org.apache.cordova.geolocation

Now, let's modify our savePhoto function in the following way:

savePhoto: function(imageURI, title) {
    var self = this;
    self.copyPhotoToPersistentStore(imageURI, function(persistentImageURI) {
        self.getCurrentPosition(function(latitude, longitude) {
            // saving goes here      
        })
    })
}

As you can see, we added another function called self.getCurrentPosition here. It...