0

在 Android 上访问 GeoLocation 插件时遇到问题。单击获取设备的 GPS 位置时,我有一个按钮,但是单击此按钮时会发生以下错误(应用程序已授予位置服务和存储权限):

在此处输入图像描述

main-view-model.js 中使用了以下代码

var Observable = require("data/observable").Observable;
var geolocation = require("nativescript-geolocation");

var mapsModule = require("nativescript-google-maps-sdk");
var pageModule = require("tns-core-modules/ui/page");

var { Accuracy } = require("ui/enums");

var dialogs = require("ui/dialogs");

function createViewModel() {
    var viewModel = new Observable();
    var watchId;
    var start_location;
    var current_location;

    viewModel.is_tracking = false;
    viewModel.latitude = 15.447819409392789;
    viewModel.longitude = 120.93888133764267;
    viewModel.zoom = 20;

    var total_distance = 0;
    var total_steps = 0;

    var locations = [];

    var mapView;
    var marker = new mapsModule.Marker();

    if (!geolocation.isEnabled()) {
        geolocation.enableLocationRequest();
    } 

    viewModel.toggleTracking = function() {

        if (geolocation.isEnabled()) {

            this.set('is_tracking', !viewModel.is_tracking);
            if (viewModel.is_tracking) {
                geolocation.getCurrentLocation(
                    {
                        desiredAccuracy: Accuracy.high, 
                        updateDistance: 5, 
                        timeout: 2000
                    }
                ).
                then(function(loc) {
                    if (loc) {
                        start_location = loc;
                        locations.push(start_location);

                        viewModel.set('latitude', loc.latitude);
                        viewModel.set('longitude', loc.longitude);

                        marker.position = mapsModule.Position.positionFromLatLng(viewModel.latitude, viewModel.longitude); 
                    }
                }, function(e){
                    dialogs.alert(e.message);
                });  

                watchId = geolocation.watchLocation(
                    function (loc) {
                        if (loc) {
                            current_location = loc;
                            locations.push(loc);

                            viewModel.set('latitude', loc.latitude);
                            viewModel.set('longitude', loc.longitude);
                            marker.position = mapsModule.Position.positionFromLatLng(viewModel.latitude, viewModel.longitude); 

                            location_count = locations.length;

                            if (location_count >= 2) {
                                var distance = Math.round(geolocation.distance(locations[location_count - 2], locations[location_count - 1]));
                                var steps = Math.round(distance * 1.3123);

                                total_distance = total_distance + distance;
                                total_steps = total_steps + steps;

                                viewModel.set('distance', "distance travelled: " + total_distance + " meters");
                                viewModel.set('steps', "steps: " + total_steps);

                            }

                        }
                    }, 
                    function(e){
                        dialogs.alert(e.message);
                    }, 
                    {
                        desiredAccuracy: Accuracy.high, 
                        updateDistance: 5, 
                        minimumUpdateTime : 5000
                    }
                );

            } else {
                geolocation.clearWatch(watchId);
                total_distance = 0;
                total_steps = 0;
                locations = [];
                viewModel.set('distance', "distance travelled: " + total_distance + " meters");
                viewModel.set('steps', "steps: " + total_steps);
            }

        } else {
            dialogs.alert("Please enable Geolocation");
        }
    }

    viewModel.getButtonStyle = function() {
        if (viewModel.is_tracking) {
            return 'bg-danger';
        }
        return 'bg-primary';
    }

    viewModel.getButtonLabel = function() {
        if (viewModel.is_tracking) {
            return 'Stop Tracking';
        }
        return 'Start Tracking';
    }

    viewModel.onMapReady = function(args) {
        mapView = args.object;
        marker.position = mapsModule.Position.positionFromLatLng(viewModel.latitude, viewModel.longitude);
        marker.userData = { index: 1 };
        mapView.addMarker(marker);
    }

    return viewModel;
}


exports.createViewModel = createViewModel;
4

1 回答 1

0

你试过了tns remove platform android吗?尝试删除和添加平台,如果您正在添加或删除插件,这有时会有所帮助。我可以查看您的 XML,单击按钮时您在调用什么函数。

我会将数组限制locations.push(loc);为最后 2 个位置。该数组将无限扩展。如果您不需要跟踪所有位置,请使用 observable 更新其最新的 2 个推送值。总距离适用于最后两个数组条目,尝试将其限制为最后两个,它可能会起作用。

于 2018-10-14T10:29:29.977 回答