Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
HTML5 getCurrentPosition almost always failing in PhoneGap on iOS
When using HTML5 geolocation in PhoneGap applications on iOS devices, getCurrentPosition may fail frequently, especially on iOS 6. This issue affects location-based functionality and requires specific configuration changes to resolve.
The Problem
PhoneGap's geolocation API generally works well on iOS, but iOS 6 introduced specific issues where getCurrentPosition consistently triggers the failure callback instead of successfully retrieving the device's location.
PhoneGap Configuration Fix
The primary solution involves modifying the PhoneGap.plist file configuration:
// In PhoneGap.plist, set the geolocation setting to: GeolocationEnabled: NO
Setting this value to YES causes memory problems on iOS 6, leading to geolocation failures. By setting it to NO, you allow the native iOS geolocation to handle requests properly.
Example Implementation
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
onSuccess,
onError,
{ timeout: 30000, enableHighAccuracy: true }
);
} else {
alert("Geolocation is not supported by this device");
}
}
function onSuccess(position) {
console.log('Latitude: ' + position.coords.latitude);
console.log('Longitude: ' + position.coords.longitude);
}
function onError(error) {
console.log('Error code: ' + error.code);
console.log('Error message: ' + error.message);
}
// Call the function
getLocation();
Apache Cordova Alternative
For better compatibility and ongoing support, consider migrating to Apache Cordova instead of PhoneGap. Older versions of Cordova contained geolocation bugs, so ensure you're using the latest version for optimal performance and reliability.
// Cordova geolocation plugin installation cordova plugin add cordova-plugin-geolocation
Troubleshooting Tips
- Increase timeout values for slower GPS acquisition
- Test on actual iOS devices rather than simulators
- Ensure location services are enabled in device settings
- Update to the latest Cordova/PhoneGap version
Conclusion
Setting PhoneGap.plist GeolocationEnabled to NO resolves iOS 6 getCurrentPosition failures. For new projects, Apache Cordova provides better long-term support and fewer geolocation issues.
