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

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

问题描述

我正在使用PhoneGap建立一个应用程式。在使用手机地理位置API的时候,我意识到API超时有两个原因,同样的错误被抛出:
1.如果用户设备上未启用GPS
2.如果启用了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?

我想知道是否有任何方式,我可以检查GPS是否在用户的设备上使用Phonegap激活,以便我可以做作为单独的检查和直接用户到设置窗口,其中通常放置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 a>。我猜,当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.

你可以尝试的另一件事是使用 diagnostic phonegap插件for android 。你必须确保你也使用其他平台的插件,但他们也都在那里。

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天全站免登陆