Detecting current postion of mobile devices using PhoneGap Geolocation API

Introduction: Geolocation is the property by which we can detect the current position of the device using latitude and longitude. We can get the location by invoking GPS(Global Positioning System) and the location obtain from network signals such as IP address, WiFi and mobile networks. PhoneGap provides the geolocation API which can be used to detect the device position by invoking the GPS. So with out using any native plugin, we can get the device geolocation simply using JavaScript.

Description: PhoneGap Geolocation API is basically based upon the W3C Geolocation API and it follows the same specification that W3C Geolocation does to detect the device latitude and longitude. So after integrating the Geolocation API, when we run the application in device it will ask for the user permission in iOS devices as per the privacy guidelines. If we cancel the permission then the app will not be able to obtain the geolocation.

Here are the steps below that we need to follow to invoke Geolocation API.

1. Include the Phonegap/Cordova javascript file inside your html page.

2.Add the eventlistner to load the function when device gets ready.

document.addEventListener("deviceready", GetGeoLocation , false);

3.Create a function to get the geolocation of current position of the device.

function  GetGeoLocation () {
  navigator.geolocation.getCurrentPosition(ShowPosition, ShowError);
}

geolocation.getCurrentPosition is the method which returns the current position of the device to the ShowPosition function with position as the parameter. And if there is any error occurred then, ShowError function is called to display the error.

We can also pass the following parameter with the geolocation.getCurrentPosition method.

 { maximumAge: 3000, timeout: 5000, enableHighAccuracy: true };

function  GetGeoLocation () {
  var options =  { maximumAge: 3000, timeout: 3000, enableHighAccuracy: true };
  navigator.geolocation.getCurrentPosition(ShowPosition, ShowError, options);
}

Here the maximumAge is used to indicate that the application is going to accept a cached position whose age is no greater than the specified time in milliseconds. The timeout attribute is used to set the maximum length of time in milliseconds that is allowed to pass from the call to getCurrentPosition() until the corresponding success function is invoked. And the enableHighAccuracy attribute is used to receive the best possible results by the application.

Note : In case of Android it is very very important to set { enableHighAccuracy: true } to find the Geolocation. It helps to receive the accurate results for latitude and longitude.

4.Create a function to display the latitude and longitude of the current position.

function ShowPosition(position) {
    alert("Latitude: " + position.coords.latitude +
          "Longitude: " + position.coords.longitude);
}

5.Create a function to handle the error.

function ShowError(error) {
   alert("Errorcode: "    + error.code    +
         "Errormessage: "+ error.message );
}

So now we are done with getting the device geolocation by obtaining the current latitude and longitude.

Summary: From the above example we learn that how to invoke the GPS and get the geolocation in mobile devices using PhoneGap Geolocation API.

Written By: Sourabha Kumar Sahoo, Software Developer, Mindfire Solutions

Advertisements

2 thoughts on “Detecting current postion of mobile devices using PhoneGap Geolocation API

  1. Pingback: Detecting current postion of mobile devices using PhoneGap Geolocation API | souravsahoo

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s