- Cordova - Best Practices
- Cordova - Whitelist
- Cordova - Vibration
- Cordova - Splash Screen
- Cordova - Network Information
- Cordova - Media Capture
- Cordova - Media
- Cordova - InAppBrowser
- Cordova - Globalization
- Cordova - Geolocation
- Cordova - File Transfer
- Cordova - File System
- Cordova - Dialogs
- Cordova - Device Orientation
- Cordova - Accelerometer
- Cordova - Device
- Cordova - Contacts
- Cordova - Camera
- Cordova - Battery Status
- Cordova - Plugman
- Cordova - Back Button
- Cordova - Events
- Cordova - Storage
- Cordova - Config.xml File
- Cordova - First Application
- Cordova - Environment Setup
- Cordova - Overview
- Cordova - Home
Cordova Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Cordova - Geolocation
Geolocation is used for getting info about device s latitude and longitude.
Step 1 - Instalpng Plugin
We can install this plugin by typing the following code to command prompt window.
C:UsersusernameDesktopCordovaProject>cordova plugin add cordova-plugin-geolocation
Step 2 - Add Buttons
In this tutorial we will show you how to get current position and how to watch for changes. We first need to create buttons that will call these functions.
<button id = "getPosition">CURRENT POSITION</button> <button id = "watchPosition">WATCH POSITION</button>
Step 3 - Add Event Listeners
Now we want to add event psteners when the device is ready. We will add the code sample below to onDeviceReady function in index.js.
document.getElementById("getPosition").addEventListener("cpck", getPosition); document.getElementById("watchPosition").addEventListener("cpck", watchPosition);
Step 3 - Create Functions
Two functions have to be created for two event psteners. One will be used for getting the current position and the other for watching the position.
function getPosition() { var options = { enableHighAccuracy: true, maximumAge: 3600000 } var watchID = navigator.geolocation.getCurrentPosition(onSuccess, onError, options); function onSuccess(position) { alert( Latitude: + position.coords.latitude + + Longitude: + position.coords.longitude + + Altitude: + position.coords.altitude + + Accuracy: + position.coords.accuracy + + Altitude Accuracy: + position.coords.altitudeAccuracy + + Heading: + position.coords.heading + + Speed: + position.coords.speed + + Timestamp: + position.timestamp + ); }; function onError(error) { alert( code: + error.code + + message: + error.message + ); } } function watchPosition() { var options = { maximumAge: 3600000, timeout: 3000, enableHighAccuracy: true, } var watchID = navigator.geolocation.watchPosition(onSuccess, onError, options); function onSuccess(position) { alert( Latitude: + position.coords.latitude + + Longitude: + position.coords.longitude + + Altitude: + position.coords.altitude + + Accuracy: + position.coords.accuracy + + Altitude Accuracy: + position.coords.altitudeAccuracy + + Heading: + position.coords.heading + + Speed: + position.coords.speed + + Timestamp: + position.timestamp + ); }; function onError(error) { alert( code: + error.code + + message: + error.message + ); } }
In example above we are using two methods − getCurrentPosition and watchPosition. Both functions are using three parameters. Once we cpck CURRENT POSITION button, the alert will show geolocation values.
If we cpck WATCH POSITION button, the same alert will be triggered every three seconds. This way we can track movement changes of the user s device.
NOTE
This plugin is using GPS. Sometimes it can t return the values on time and the request will return timeout error. This is why we specified enableHighAccuracy: true and maximumAge: 3600000. This means that if a request isn t completed on time, we will use the last known value instead. In our example, we are setting maximumAge to 3600000 milpseconds.
Advertisements