简体   繁体   English

使用Cordova进行位置服务

[英]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. 要停止此任务,请使用主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. 我可以使用本机android命令实现此功能。 But I want to implement this using cordova and I can't figure out a way to do this.. 但是我想使用cordova来实现这一点,但我想不出一种方法来实现。

If this task cannot be done using cordova, can I do it using native android and inject to the cordova app..?? 如果此任务无法使用cordova完成,我可以使用本机android来完成并注入cordova应用程序吗?

Please help.. 请帮忙..

You can do this using the ng-cordova geolocation watcher, it watches geolocation properties such as lat, lon, and speed. 您可以使用ng-cordova地理位置观察器来执行此操作,它会观察地理位置属性,例如纬度,经度和速度。

first intall ng-cordova and inject it 首先安装ng-cordova并注入

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. 当您查看他们的地理位置信息时,您可以将其推送到本地数据库,例如http://pouchdb.com/或couch db或服务器数据库。 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();
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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