NativeScript Geolocation失败"未被捕获的异常发生在" main"线。 com.tns.NativeScriptException:调用js方法onClick失败"

时间:2018-02-14 02:47:01

标签: android geolocation nativescript nativescript-plugin

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

enter image description here

以下代码用于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;

1 个答案:

答案 0 :(得分:0)

您尝试过tns remove platform android吗?尝试删除和添加平台,如果要添加或删除插件,有时会有所帮助。我能看到您的XML,单击按钮时您要调用什么功能。

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