使用 PhoneGap 检查 GPS 是否启用 [英] Use PhoneGap to Check if GPS is enabled

查看:25
本文介绍了使用 PhoneGap 检查 GPS 是否启用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PhoneGap 构建应用程序.在使用 phonegap 的 Geolocation API 时,我意识到 API 超时有两个原因,并且抛出了相同的错误:1. 如果用户设备上未启用 GPS2.如果开启了GPS,无法获取到用户的位置(可能有多种原因,GPS故障,天气不明等)

I am building an app using PhoneGap. While using the Geolocation API of phonegap I realized that the APIs time out for two reasons and the same error is thrown: 1. If GPS is not enabled on the user's device 2. If GPS is enabled and is not able to get the location of the user (could be several reason, faulty GPS, unclear weather and so on)

我在区分原因时遇到了问题?关于如何做到这一点的任何想法?

I am having an issue in differentiating the cause? Any thoughts on how to do it?

我想知道是否有任何方法,我可以使用 Phonegap 检查用户设备上的 GPS 是否处于活动状态,以便我可以将其作为单独的检查并将用户引导到通常放置 GEO 设置的设置窗口.不知道该怎么做?自定义 phonegap 插件可能是?

I was wondering if there is any way, I can check if GPS is active on user's device using Phonegap, so that i can do that as a separate check and direct user to the settings window where typically GEO settings are placed. Not sure how to do that? a custom phonegap plugin may be?

推荐答案

您可以在 PositionError 参数 geolocationError 在您致电 getCurrentPosition.我猜测当 gps 未启用时它会是 PositionError.PERMISSION_DENIED,而当 gps 启用时它会是 PositionError.POSITION_UNAVAILABLE 或 PositionError.TIMEOUT 但还有其他问题.

You could check the error code in PositionError parameter of geolocationError in your call to getCurrentPosition. I am guessing that it will be PositionError.PERMISSION_DENIED when gps is not enabled, and PositionError.POSITION_UNAVAILABLE or PositionError.TIMEOUT when gps is enabled but there are other issues.

请注意,这取决于平台.您可能不得不编写一条人为的错误消息,指出无法获取当前位置.要么 GPS 信号较弱,要么 GPS 已关闭".

Note that this is platform dependent. You would probably have to write a contrived error message that says "Could not get the current position. Either GPS signals are weak or GPS has been switched off".

您可以尝试的一件事是调用 getCurrentPosition 时的超时时间非常短,比如 1 毫秒.如果它说权限被拒绝,您可以断定 gps 已禁用,如果超时,您可以假设 gps 已启用.我没有时间对此进行测试,您可以根据测试结果编辑此答案.

One thing you can try is to call getCurrentPosition with an incredibly small timeout, say 1 ms. If it says permission denied, you can conclude that gps is disabled and if it times out, you can assume that gps is enabled. I do not have time to test this, you could probably edit this answer with result of your tests.

您可以尝试的另一件事是使用 Android 诊断 phonegap 插件.您必须确保也使用适用于其他平台的插件,但它们也大部分都在那里.

Another thing you can try is to use the diagnostic phonegap plugin for android. You will have to make sure you use the plugins for the other platforms also, but they are all also mostly there.

这篇关于使用 PhoneGap 检查 GPS 是否启用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆