The Problem
I recently ran into the issue of “GPS Provider Disabled” when using Apache Cordova on Android. After reading many threads, the fix seemed to always be to set the permissions in config.xml with the following code:
1.
"org.apache.cordova.geolocation"
/>
After doing that, you would need to check Settings –> Location and make sure it is on.
If it didn’t work after that, well you were pretty much on your own…
The Resolution
After digging I found that if I include enableHighAccuracy to false, then it would use Assisted GPS rather than satellite positioning. Sure, the results may not be as high as you would like but it is better than an ugly dialog box saying “GPS Provider Disabled”.
Here is a full sample:
01.
02.
<
html
>
03.
<
head
>
04.
<
title
>Device Properties Exampletitle
>
05.
06.
<
script
type
=
"text/javascript"
charset
=
"utf-8"
src
=
"cordova-2.5.0.js"
>script
>
07.
<
script
type
=
"text/javascript"
charset
=
"utf-8"
>
08.
09.
// Wait for Cordova to load
10.
//
11.
document.addEventListener("deviceready", onDeviceReady, false);
12.
13.
var watchID = null;
14.
15.
// Cordova is ready
16.
//
17.
function onDeviceReady()
18.
// Changed this from true to false to prevent dialog popup on Android devices.
19.
var options = enableHighAccuracy: false ;
20.
watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);
21.
22.
23.
// onSuccess Geolocation
24.
//
25.
function onSuccess(position)
26.
var element = document.getElementById('geolocation');
27.
element.innerHTML = 'Latitude: ' + position.coords.latitude + '
28.
' +
29.
'Longitude: ' + position.coords.longitude + '
30.
' +
31.
'<
hr
/>' + element.innerHTML;
32.
33.
34.
// clear the watch that was started earlier
35.
//
36.
function clearWatch()
37.
if (watchID != null)
38.
navigator.geolocation.clearWatch(watchID);
39.
watchID = null;
40.
41.
42.
43.
// onError Callback receives a PositionError object
44.
//
45.
function onError(error)
46.
alert('code: ' + error.code + '\n' +
47.
'message: ' + error.message + '\n');
48.
49.
50.
script
>
51.
head
>
52.
<
body
>
53.
<
p
id
=
"geolocation"
>Watching geolocation...p
>
54.
<
button
onclick
=
"clearWatch();"
>Clear Watchbutton
>
55.
body
>
56.
html
>
Wrap-Up
I hope that helps anyone out there that is having problems with this!
Không có nhận xét nào:
Đăng nhận xét