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

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
Request for Proposal
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
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.