首页 > 解决方案 > 如何使用 Nativescript 获取两个位置之间的距离

问题描述

当我有两个位置时,我想获得距离。

首先:我从geoLocation.

geoLocation.isEnabled().then(enabled => {
    console.log('enabled', enabled)
    if (!enabled) {
        geoLocation.enableLocationRequest().then(() => geoLocation.watchLocation(location => {
            this.currentGeoLocation = location;
        }, error => {
            alert(error);
            console.log('error', error)
        }, {
                desiredAccuracy: 3,
                updateDistance: 10,
                minimumUpdateTime: 1000 * 1
            }));
    } else {
        geoLocation.watchLocation(location => {
            this.currentGeoLocation = location;

        }, error => {
            alert(error);
            console.log('error', error)
        }, {
                desiredAccuracy: 3,
                updateDistance: 10,
                minimumUpdateTime: 1000 * 1
            });
    }
}

this.currentGeoLocation.longitudethis.currentGeoLocation.latitude获取坐标。

第二:

从这个功能:

   getallprod() {
        this.ws.getallprod().subscribe(
            items => {
                this.items = items;
                console.log(this.items)
            },
            err => console.error('err', err),
            () => console.log('error')
        );
    }

我得到这个带有其他坐标的 JSON:

JS: [{
JS:   "pro_id": "6969",
JS:   "name": "Pharmacy",
JS:   "longitude": 41.3257,
JS:   "latitude": 19.8029
JS: }, {
JS:   "pro_id": "525",
JS:   "name": "Test ",
JS:   "longitude": 41.3253,
JS:   "latitude": 19.8013
JS: }]

我在这里看到如何计算距离

function getDistance(loc1, loc2) {
loc1 = this.currentGeoLocation.longitude +this.currentGeoLocation.latitude;
loc2 = this.items.longitude+this.items.latitude;
    console.log("Distance between loc1 and loc2 is: " + distance(loc1, loc2));
}

错误:无法在数字“61.126601199999996”上创建属性“android”

标签: javascripttypescriptgeolocationnativescriptgetdistance

解决方案


推荐阅读