ionic tutorial - Cordovageolocation ionic | Ionic Cordova Geolocation - ionic - ionic tutorial - ionic framework
- The Cordova Geolocation provides information about the device's location, such as latitude and longitude. Common sources of location information include Global Positioning System (GPS) and location inferred from network signals such as IP address, RFID, WiFi and Bluetooth MAC addresses, and GSM/CDMA cell IDs.
- This API is based on the W3C Geolocation API Specification, and only executes on devices that don't already provide an implementation.
How to use Cordova Geolocation in Ionic Framework
- There is a simple way to use geolocation plugin. We need to install this plugin from the command prompt window.
- The following controller code is using two methods. The first one is getCurrentPosition and it will show us current latitude and longitude of the user’s device.
- The second one is watchCurrentPosition that will return current position of the device when position is changed.
Controller Code
- You also noticed posOptions and watchOptions objects. We are using timeout to adjust maximum length of time that is allowed to pass in milliseconds and enableHighAccuracy is set to false.
- It can be set to true to get the best possible results but sometimes it can lead to some errors.
- There is also maximumAge option that can be used to show how old position is accepted. It is using milliseconds, the same as timeout option.
- When we start our app, and open the console it will log the latitude and longitude of the device. When our position is changed, the lat and long values will change.