Saturday, 14 May 2016

Geolocation plugin in Phonegap

Geolocation: 
           This provides information about the devices's location such as latitude and longitude based on devices's Global Positioning System(GPS) sensor and inferred from network signals such as IP address, RFID, WiFi and Bluetooth MAC addresses.
          But there is no guarantee that the API will return actual position of the device.

First, need to install the plugin in your project directory by using below command:

>> phonegap plugin add org.apache.cordova.geolocation


3 methods:

1. geolocation.getCurrentPosition
2. geolocation.watchPosition
3. geolocation.clearWatch

1. geolocation.getCurrentPosition: It is an asynchronous function. It returns the devices's current position to the geolocationSuccess callback with a Position object as a parameter. If there is an error, the geolocationError is passed with PositionError object.

navigator.geolocation.getCurrentPosition(geolocationSuccess,
                                                                   geolocationError,
                                                                   geolocationOptions);

geolocatoinOptions ---> For extra options like timeout

Sample Example:


navigator.geolocation.getCurrentPosition(geolocationSuccess,geolocationError);

function geolocationSuccess(position)
{
        alert("Latitude: " + position.coords.latitude + "\n" + 
                 "Longitude: " + position.coords.longitude + "\n" + 
                 "Altitude: " + position.coords.altitude + "\n" + 
                 "Accuracy: " + position.coords.accuracy + "\n" + 
                 "Altitude Accuracy: " + position.coords.altitudeAccuracy + "\n" + 
                 "Heading: " + position.coords.heading + "\n" + 
                 "Speed: " + position.coords.speed + "\n" + 
                 "Timestamp: " + position.coords.timestamp + "\n" + 
               );
}

function geolocationError(positionError)
{
         alert("Error Code" + positionError.code + "\n" + 
                 "Error Message" + positionError.message + "\n" + 
                );
}

2. geolocation.watchPosition: It is an asynchronous function. It returns the current position of the device when a change in position detected. When the device found new location, the geolocationSuccess callback returns position object as a parameter. If there is an error, the geolocationError callback executes with a positionError object as the parameter.


var watchId = navigator.geolocation.watchPosition(geolocationSuccess,
                                                                                   geolocationError,
                                                                                  geolocationOptions);

Note: You can write above example for this, the only difference is above geolocationSuccess calls one time, but in this whenever the position changes.

3. geolocation.clearWatch: It stops watching whenever device changes by using watchId  mentioned in second method.

navigator.geolocation.clearWatch(watchId);





Please let me know any help from Phonegap side.

No comments:

Post a Comment