Nativescript 位置插件仅打开位置设置

Nativescript location plugin only opens location settings

我用 nativescript 和 angular 写了一些代码来观看位置。

但即使我设备上的 GPS 已打开,geolocation.isEnabled() 显示 false

另外,当 运行 geolocation.watchLocation(..) 我的设备只打开位置设置。并返回错误消息:

Error: Location service is not enabled or using it is not granted.

有人知道我的应用无法使用定位的原因吗?

我的app.compoment.html:

<GridLayout columns="*,*" rows="auto,auto,auto">
    <Label text="Geolocation" class="title" row="0" colSpan="2"></Label>
    <Button row="1" col="0" text="Start Tracking" (tap)="startTracking()"></Button>
    <Button row="1" col="1" text="Stop Tracking" (tap)="stopTracking()"></Button>
    <Label row="2" col="0" text="Latitude: {{latitude}}"></Label>
    <Label row="2" col="1" text="Longitude: {{longitude}}"></Label>
</GridLayout>

我的app.component.ts:

@Component({
selector: "my-app",
templateUrl: "app.component.html",
})
export class AppComponent {

latitude = "42";
longitude = "5";
watchId = 0;

public startTracking() {
    console.log("Start Tracking");

    /* Check if GPS is enabled. */
    if (geolocation.isEnabled()) {
        console.log("GPS is enabled.");
    } else {
        console.log("GPS is disabled.");
    }

    /* Get Location */
    this.watchId = geolocation.watchLocation(function (loc) {
            if (loc) {
                console.log("Current location is: " + loc);
            }
        }, function (e) {
            console.log("Error: " + e.message);
        },
        {desiredAccuracy: enums.Accuracy.any, updateDistance: 10, minimumUpdateTime: 1000});
}

public stopTracking() {
    console.log("Stop Tracking");
    if (this.watchId) {
        geolocation.clearWatch(this.watchId);
        this.watchId = 0;
    }
}
}

您是否尝试过在您的设备上明确设置地理定位服务的权限?

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

// in page.html
<StackLayout>
    <Button text="enable Location" (tap)="enableLocationTap()"></Button>
</StackLayout>

触发按钮后,应用程序应显示带有 allow/cancel 样式消息的权限弹出窗口。