2016-07-18 37 views
0

我用一些nativescript和angular编写了一些代码来观察位置。Nativescript位置插件只能打开位置设置

但即使我的设备上的GPS开启,geolocation.isEnabled()虚假

另外,运行时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; 
    } 
} 
} 

回答

1

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

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

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

触发按钮后,应用程序应显示允许/取消样式消息的权限弹出。

+0

我会尝试你的解决方案 – Julisch

+0

谢谢你。有效。 – Julisch

+0

没有办法在应用程序安装时自动授予应用程序这些权限吗?举例来说,安装pokemon go时,它已经表示如果你安装了这个应用程序,它就可以访问你的位置服务,而在你的应用程序中你不需要它。 – Starwave