Track Your Mobile Using Cordova

Posted By : Akhil Dhiman | 16-Dec-2014
Track Your Mobile Using Cordova

Here I will show you how one can track his device using Cordova.
Tracking of device is very useful these days. As everyone is using smartphones these days this will help one to track his/ her lost phone.
 
For this we need to install cordova plugin name "Cordova Plugin geolocation". You can check this plugin from below link.

https://github.com/apache/cordova-plugin-geolocation

To add this plugin in your project follow the following procedure :

Using terminal go in your project directory, from inside your Cordova project you have to enter the following command.
 cordova plugin add org.apache.cordova.geolocation
 
This plugin will use your GPS to get your current location.
 
Below is the method to get your location using plugin :
navigator.geolocation.getCurrentPosition(geolocationSuccess, [geolocationError],[geolocationOptions]);                                    
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.timestamp+ '\n');
}
 
You Need to store make an recursive Function that will make an call to get current location after 5 mins and save data to local storage in JSON format like below :
 var locationObj = [
    {
        "position": "1",
        "lat": position.coords.latitude,
        "long": position.coords.longitude
    },
    {
        "position": "2",
        "lat": position.coords.latitude,
        "long": position.coords.longitude
    }
]
window.localStorage.setItem("locationObj",JSON.stringify(locationObj));
 
where position.coords.latitude is latitude of current Position and position.coords.longitude in longitude of current position.
 
Now we need to make an function set interval function that will make an call to server after every 30 mins and fetch JSON data from local storage and sent it to the server after getting response from server it will reset local storage and start fetching new locations.
 
The Ajax call will be like below:
 var dataToSend = window.localStorage.getItem("locationObj");
dataToSend = window.btoa(dataToSend);
//urlToHit is the url of web service
$.ajax({
    type: "POST",
    url: urlToHit,
    timeout: 30000 ,
    data: dataToSend
    }).done(function( msg ) {
   	window.localStorage.removeItem("locationObj");
    }).fail(function(jqXHR, textStatus, errorThrown){
        console.log("Network Error!!!!");
    });
 
We can check network status of device using Cordova plugin name Network Information. If your device is offline then it will wait for the device to be online and whenever device is online then it will send your locations to server.
 
Thanks

About Author

Author Image
Akhil Dhiman

Akhil is an iPhone and Android application developer with experience in PhoneGap and Swift(Native iOS). Akhil has good experience working with JavaScript, jQuery and Underscore as well.

Request for Proposal

Name is required

Comment is required

Sending message..