【问题标题】:Nativescript location plugin only opens location settingsNativescript 位置插件只打开位置设置
【发布时间】:2016-07-18 18:59:14
【问题描述】:

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

【问题讨论】:

    标签: nativescript angular2-nativescript


    【解决方案1】:

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

    public enableLocationTap() { 
        if (!geolocation.isEnabled()) {
            geolocation.enableLocationRequest();
        }
    }
    
    // in page.html
    <StackLayout>
        <Button text="enable Location" (tap)="enableLocationTap()"></Button>
    </StackLayout>
    

    在触发按钮后,应用应该显示一个带有允许/取消样式消息的权限弹出窗口。

    【讨论】:

    • 我会试试你的解决方案
    • 有没有办法在应用安装时自动授予应用这些权限?例如,在安装 pokemon go 时,它已经说如果您安装此应用程序,它将可以访问您的位置服务,并且在应用程序内部您不必请求它。
    • 是的,您可以 - 在 AndroidManifest.xml 中请求对 Nougat (Android 7) 以下的所有 Android 版本的权限。当您需要明确授予权限时(例如 Pokemon Go 示例或所有 Android-7 pps),您可以在 app.js 中的 application.start() 方法之前执行此操作。您可以使用例如 nativescript-permissions 插件
    • 谢谢,是的,在app/App_Resources/Android/AndroidManifest.xml 中找到了清单。我可以看到uses-permission 只允许互联网和读/写外部存储,但是在 nativescript 位置文章中,他们说他们添加了这个权限。似乎它是即时完成的,在清单转换为 android 代码时完成。如果其他人想知道为什么他们的应用程序会出现该错误 - 尝试删除手机上的应用程序。从 npm 包含 nativescript-geolocation 后,您必须完全重建应用程序。现在在设置中我可以在应用信息中看到,我的应用现在可以访问精确位置:)
    • 哦,我一直收到错误Error: API key not found. Check that &lt;meta-data android:name="com.google.android.geo.API_KEY" android:value="your API key" /&gt; is in the &lt;application&gt; element of AndroidManifest.xml。不知道为什么,但是现在将您的密钥放在 app/App_Resources/Android/values/nativescript_google_maps_api.xml 中是不够的,所以我做了错误提示的操作 - 将该元素作为 &lt;application&gt; 元素的第一个子元素并开始工作。
    猜你喜欢
    • 1970-01-01
    • 2014-04-07
    • 2021-04-15
    • 1970-01-01
    • 2019-12-12
    • 2014-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多