简体   繁体   English

如何检查GPS是否启用Ionic2(地理定位)?

[英]How to check GPS is enable or not with Ionic2 (Geolocation)?

I use plugin cordova Geolocation for ionic 2 but I need to know if the GPS is enabled or disabled . 我使用插件cordova Geolocation for ionic 2但我需要知道GPS是启用还是禁用。 I don't know how make this. 我不知道怎么做到这一点。

Please Help Me ;) Thanks! 请帮助我;)谢谢!

The diagnostic plugin has your back with a lot of functionality to check for GPS mode, location status and more. 诊断插件背面有很多功能可以检查GPS模式,位置状态等。 Check the included link to know more about it. 查看附带的链接以了解更多信息。

import { Diagnostic } from '@ionic-native/diagnostic';

constructor(private diagnostic: Diagnostic) { }

...

let successCallback = (isAvailable) => { console.log('Is available? ' + isAvailable); };
let errorCallback = (e) => console.error(e);

this.diagnostic.isLocationEnabled().then(successCallback).catch(errorCallback);

// only android
this.diagnostic.isGpsLocationEnabled().then(successCallback, errorCallback);

You can also use the location-accuracy plugin which is lighter but it's missing other options available on the diagnostic plugin. 您还可以使用更轻的位置精度插件,但它缺少诊断插件上提供的其他选项。 I'm currently using this plugin to enable gps on android and to set the mode. 我目前正在使用这个插件在Android上启用gps并设置模式。 I'll suggest to also look at the doc of this plugin. 我建议你也看一下这个插件的文档。

I had good luck with this plugin http://devfanaticblog.com/background-geolocation-with-apache-cordova/ cordova plugin add cordova-plugin-mauron85-background-geolocation 我很喜欢这个插件http://devfanaticblog.com/background-geolocation-with-apache-cordova/ cordova plugin add cordova-plugin-mauron85-background-geolocation

Might be extreme overkill for what you need though. 尽管你需要什么,但可能是极端矫枉过正。 You could always just call getcurrentposition() and check if you get a valid result. 你总是可以调用getcurrentposition()并检查你是否得到了有效的结果。

navigator.geolocation.getCurrentPosition(success[, error[, options]])

I am not certain how iPhones treat it, but Android can be downright evil now. 我不确定iPhone如何对待它,但Android现在可能是彻头彻尾的邪恶。 Because the user will be prompted to give permission to their GPS if you ask to use it. 因为如果您要求使用它,将提示用户授予其GPS权限。 If they choose no, then it could be enabled, but unavailable to you. 如果他们选择否,则可以启用它,但您无法使用。 You also can't easily tell what precision you are getting. 你也不能轻易分辨出你的准确度。 You might be getting wifi or mobile location instead of GPS location. 你可能会获得无线或移动位置而不是GPS位置。

This plugin covers all those scenarios, however, it has an odd bug that it doesn't work properly in the android emulator. 这个插件涵盖了所有这些场景,然而,它有一个奇怪的错误,它在Android模拟器中无法正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM