我用 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;
}
}
}