---
title: "Apache Cordova - Geolocation"  
description: "The measurement of the geographical location of an object, such as a mobile phone, or an Internet-connected PC is its Geolocation. Basically, it invol"  
author: "Anonymous User"  
published: 2017-04-27  
updated: 2018-03-19  
canonical: https://www.mindstick.com/blog/11365/apache-cordova-geolocation  
category: "apache cordova"  
tags: ["mobile development", "cordova", "apache cordova"]  
reading_time: 4 minutes  

---

# Apache Cordova - Geolocation

The measurement of the geographical location of an object, such as a [mobile phone](https://www.mindstick.com/articles/95862/mobile-phone-technology-has-revolutionized-the-marketing-industry), or an Internet-connected PC is its Geolocation. Basically, it involves the [generation](https://www.mindstick.com/blog/300788/why-is-heart-attack-increasing-in-the-younger-generation) of a set of geographical coordinates and is [closely related](https://answers.mindstick.com/qa/52066/the-52nd-amendment-to-the-constitution-of-india-is-most-closely-related-to) to the use of positioning systems, but its usefulness is enhanced by the use of these coordinates to determine a [meaningful](https://yourviews.mindstick.com/story/1909/6-meaningful-ways-to-celebrate-christmas-this-year) location, such as a street address.

Geolocation feature is used for getting device's real-time [latitude and longitude](https://answers.mindstick.com/qa/50053/what-are-latitude-and-longitude-in-geographical-map-and-how-can-use-it) with respect to its current position.

**Step 1 – Install Geolocation Plugin**

Open **[command prompt](https://answers.mindstick.com/qa/51716/how-to-make-bootable-pendrive-using-cmd-windows-command-prompt)** window and type the following code to install this plugin:

```
C:\Users\username\Desktop\CordovaProject>cordova plugin add cordova-plugin-geolocation
```

**Step 2 - Adding Buttons**

In this article you will be able to know how to get current position and how it changes with the change in location. First you need to create buttons that will call these functions.

```
<button id = "currentPosition">CURRENT POSITION</button>
<button id = "changePosition">WATCH POSITION</button>
```

**Step 3 - Adding [Event Listeners](https://www.mindstick.com/forum/159117/how-do-you-handle-events-using-jquery-and-attach-event-listeners-to-elements)**

Now when the device is ready, you have to add event listeners. Add the following code to onDeviceReady function in index.js file.

```
document.getElementById("currentPosition").addEventListener("click", currentPosition); document.getElementById("changePosition").addEventListener("click", changePosition); 
```

**Step 3 - Creating Functions**

The two functions have to be created for two event listeners.

One will be used for getting the current position and the other for changed position.

```
function currentPosition() {
   var options = {  
      enableHighAccuracy: true,
      maximumAge: 3600000
   }
   var watchID = navigator.geolocation.getCurrentPosition(onSuccess, onError, options);
   function onSuccess(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');
   };
   function onError(error) {
      alert('code: ' + error.code + '\n' + 'message: ' + error.message + '\n');
   }
}
function changePosition() {
   var options = {
      maximumAge: 3600000,
      timeout: 3000,
      enableHighAccuracy: true,
   }
   var watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);
   function onSuccess(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');
   };
   function onError(error) {
      alert('code: ' + error.code + '\n' +'message: ' + error.message + '\n');
   }
}
```

In example above we are using two methods − CurrentPosition and changePosition. Both the functions are using three parameters. when we click on CURRENT POSITION button, the alert will show geolocation values.

\

![Apache Cordova - Geolocation](https://www.mindstick.com/blogs/b98162d0-4299-457e-b5dc-6330b9bdc086/images/18a225e6-9470-4a51-902c-16a749479ac3.png)

\

And when we click WATCH POSITION button, the same alert will be fired in every three seconds. This way we can track movement changes of the user's device.

**NOTE:**

This plugin uses GPS. Sometimes it can't return the values on time and the request will give time out error. That is why we specified enableHighAccuracy: true and maximumAge: 3600000.

This means that if request isn't completed on time, we will use the last known value instead.

In our example we are setting maximumAge to 3600000 milliseconds.

**Also Read our previous posts:**

Cordova – Displaying Device Info

Cordova- camera plugin

[Cordova- Battery Plugin](https://www.mindstick.com/blog/11357/cordova-battery-plugin)

---

Original Source: https://www.mindstick.com/blog/11365/apache-cordova-geolocation

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
