問題描述
我在 Geolocation Cordova 插件 (org.apache.cordova.geolocation) 上遇到了很多問題.它在 iOS 上運行良好,但在 Android 上根本不行.
I've had quite some issues with the Geolocation Cordova plugin (org.apache.cordova.geolocation). It works fine on iOS, but it doesn't at all on Android.
據我了解,該插件曾經包含原生 Android 代碼,但在某些時候被刪除了,因為它太錯誤/太慢了,而且原生 Web HTML5 實現更加穩定和快速.
As I understand, the plugin used to include native Android code, but this was removed at some point, because it was too buggy/slow and the native web HTML5 implementation was much more stable and fast.
如果我使用仍然具有本機代碼的最新插件版本 (0.3.2),它確實可以工作(但速度很慢,實際上并非總是如此).但是當它返回時,位置對象總是被填充.
If I use the latest plugin version (0.3.2) which still has the native code, it does work (but slow and indeed, not always). But when it does return, the position object is always populated.
如果我使用最新的插件版本 (1.0.1),getCurrentPosition() 會立即返回一個空對象 ({}).它不會拋出錯誤.
If I use the latest plugin version (1.0.1), the getCurrentPosition() immediately returns with an empty object ({}). It does not throw an error.
如果我完全刪除插件,并手動將權限添加到 Android 項目:
If I remove the plugin completely, and add the permissions manually to the Android project:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
同樣的事情也會發生.我只是無法讓它工作,但這沒有意義.Android 控制臺中不會顯示任何錯誤.有什么想法嗎?
The same thing happens. I just can't get it to work, but it doesn't make sense. No errors are displayed in the Android console. Any thoughts?
推薦答案
好的,調試了好久,發現問題了.顯然,getCurrentPosition() 函數在 Android 中返回一個特殊"對象,當使用 JSON.stringify() 時,它的計算結果為 {}.如果我將原始返回對象輸出到控制臺,結果證明它根本不是空的.
OK, after a long long time of debugging, I found the problem. Apparently, the getCurrentPosition() function returns a 'special' object in Android, which evaluates to {} when using JSON.stringify(). If I outputted the raw return object to the console, it turned out it wasn't empty at all.
因此,在荒謬的調整之后修復了我的代碼:
So, following ridiculous adjustments fixed my code:
navigator.geolocation.getCurrentPosition(function (position) {
var positionObject = {};
if ('coords' in position) {
positionObject.coords = {};
if ('latitude' in position.coords) {
positionObject.coords.latitude = position.coords.latitude;
}
if ('longitude' in position.coords) {
positionObject.coords.longitude = position.coords.longitude;
}
if ('accuracy' in position.coords) {
positionObject.coords.accuracy = position.coords.accuracy;
}
if ('altitude' in position.coords) {
positionObject.coords.altitude = position.coords.altitude;
}
if ('altitudeAccuracy' in position.coords) {
positionObject.coords.altitudeAccuracy = position.coords.altitudeAccuracy;
}
if ('heading' in position.coords) {
positionObject.coords.heading = position.coords.heading;
}
if ('speed' in position.coords) {
positionObject.coords.speed = position.coords.speed;
}
}
if ('timestamp' in position) {
positionObject.timestamp = position.timestamp;
}
// Use the positionObject instead of the position 'object'
alert(JSON.stringify(positionObject));
}
iOS 無需上述調整即可正常工作,但由于我的應用程序是 Phonegap 應用程序,所以我總是應用上述內容.
iOS works fine without above adjustments, but as my app is a Phonegap application, I always apply the above.
這篇關于Cordova Geolocation 插件在 Android 上返回空位置對象的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!