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.
  •  ionic geo location
  • This API is based on the W3C Geolocation API Specification, and only executes on devices that don't already provide an implementation.
 ionic geo location

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.
C:\Users\Username\Desktop\MyApp>cordova plugin add cordova-plugin-geolocation
Click below button to copy the code. From wikitechy - ionic tutorial - ionic framework tutorial - team
  • 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

.controller('MyCtrl', function($scope, $cordovaGeolocation) {
   var posOptions = {timeout: 10000, enableHighAccuracy: false};
   $cordovaGeolocation
   .getCurrentPosition(posOptions)
	
   .then(function (position) {
      var lat  = position.coords.latitude
      var long = position.coords.longitude
      console.log(lat + '   ' + long)
   }, function(err) {
      console.log(err)
   });

   var watchOptions = {timeout : 3000, enableHighAccuracy: false};
   var watch = $cordovaGeolocation.watchPosition(watchOptions);
	
   watch.then(
      null,
		
      function(err) {
         console.log(err)
      },
		
      function(position) {
         var lat  = position.coords.latitude
         var long = position.coords.longitude
         console.log(lat + '' + long)
      }
   );

   watch.clearWatch();

})
Click below button to copy the code. From wikitechy - ionic tutorial - ionic framework tutorial - team
  • 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.

Related Searches to Cordovageolocation ionic | Ionic Cordova Geolocation