简体   繁体   中英

Location Service using Cordova

I want to check the location and store the required data in the internal storage of the mobile. To stop this task, use has to tap stop button in the main ui. As this task started, no matter the state of the app, that task should do its work.

I can implement this using native android command. But I want to implement this using cordova and I can't figure out a way to do this..

If this task cannot be done using cordova, can I do it using native android and inject to the cordova app..??

Please help..

You can do this using the ng-cordova geolocation watcher, it watches geolocation properties such as lat, lon, and speed.

first intall ng-cordova and inject it

bower install ngCordova

angular.module('myApp', ['ngCordova'])

then install the geolocation plugin

cordova plugin add org.apache.cordova.geolocation

then you want to create a watcher, make sure to fire it after a device ready event or it will cause problems

This is what device ready function looks like

document.addEventListener("deviceready", function () {
  $cordovaPlugin.someFunction().then(success, error);
}, false);

here is the watcher controller:

module.controller('GeoCtrl', function($cordovaGeolocation) {


  var watchOptions = {
    frequency : 1000,
    timeout : 3000,
    enableHighAccuracy: false // may cause errors if true
  };

  var watch = $cordovaGeolocation.watchPosition(watchOptions);
  watch.then(
    null,
    function(err) {
      // error
    },
    function(position) {
      var lat  = position.coords.latitude
      var long = position.coords.longitude
  });


  watch.clearWatch();
  // OR
  $cordovaGeolocation.clearWatch(watch)
    .then(function(result) {
      // success
      }, function (error) {
      // error
    });
});

As you watch their geolocation info you can push it into a local database like http://pouchdb.com/ or couch db or a server database. If you want to use this in any state of the app you can make it into a service, here is an example in a app i built

service.watchSpeed = function () {
        console.log('watcher');
        ionic.Platform.ready(function () {
            var watchOptions = {
                frequency: 15 * 60 * 1000,
                timeout: 1 * 60 * 1000,
                enableHighAccuracy: true // may cause errors if true
            };

            service.watch = $cordovaGeolocation.watchPosition(watchOptions);
            service.watch.then(
                null,
                function (err) {
                    service.watchSpeed();
                },
                function (position) {
                    if (service.maxspeed.ToUseApp !== 0) {
                        var lat = position.coords.latitude;
                        var long = position.coords.longitude;
                        var speed = position.coords.speed;
                        service.speed = speed;
                        if (speed > service.maxspeed.ToUseApp) {
                            $state.go('overspeed');
                        }
                        if ($ionicHistory.currentStateName() === 'overspeed' && speed < service.maxspeed.ToUseApp) {
                            $ionicHistory.goBack();
                        }
                    } else {
                        console.log('speed watcher has been killed, why master??');
                    }
                });
        });
    };

then in my home controller i call the watcher

ionic.Platform.ready(function () {
                ffService.getMaxSpeed();
            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM