Phonegap/Cordova geolocation not working on Android

我是研究僧i 提交于 2019-12-02 19:06:50
ASP Force

Try options this way to see if it works for you as it has for me:

var options = { enableHighAccuracy: true };

navigator.geolocation.getCurrentPosition(onSuccess, onError, options);

Try this,

Delete the geolocation plugin

cordova plugin rm org.apache.cordova.geolocation

However, ensure that AndroidManifest.xml and config.xml properties remains as such

(in app/res/xml/config.xml)
<feature name="Geolocation">
    <param name="android-package" value="org.apache.cordova.GeoBroker" />
</feature>


(in app/AndroidManifest.xml)
<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" />

And for iOS in config.xml

<feature name="Geolocation">
    <param name="ios-package" value="CDVLocation" />
</feature>

Then, build the application using CLI and run the application on device/emulator. You would find that you now get Error code as well as the message onError for getlocation.

MORE INFO: Even after doing the above steps you might get the timeout message consistently even after you setting {timeout:10000}. The solution for this is quite funny, just switch off your device and turn it on and run the application again. You should be surprised to see that working, just like me!

My solution was to use the native HTML5 geolocation support for android/samsung if it existed:

// BEFORE the deviceready event has fired:

// Check if HTML5 location support exists
app.geolocation = false;
if(navigator.geolocation) {
  app.geolocation = navigator.geolocation;
} 

// AFTER the deviceready event:

if(app.geolocation) {
  var locationService = app.geolocation; // native HTML5 geolocation
}
else {
  var locationService = navigator.geolocation; // cordova geolocation plugin
}

locationService.getCurrentPosition(
    function(pos) {

    },
    function(error) {

    }, 
    {enableHighAccuracy: true, timeout: 15000}
);

For all the samsung devices that I tested with, enableHighAccuracy had to be 'true' for this to work. With non-samsung devices such as the Nexus 7, enableHighAccuracy could be set to 'false' as well.

Maxirus

Using Phonegap and Genymotion, the successHandler was never being called when I tried to getCurrentPosition. Adding the options:

{enableHighAccuracy: true, timeout: 2*1000, maximumAge: 0}

fixed this for me.

According to https://issues.apache.org/jira/browse/CB-5977, Android plugin support has been removed. Yi Ming Kuan said "The reason we went with CB-5977 is that the webview on newer Android versions will get GPS locks more quickly than the native API, hence its removal. Adding the plugin on Android just adds the necessary permissions to use HTML5 Geolocation in your app."

  • check your config.xml file(s).

delete this line

<preference name="permissions" value="none"/>
  • look into your AndroidManifest.xml

check that all <uses-permission...> lines and the <uses-sdk...> line are located before the <application...> tag.

after that uninstall the application manually on the phone and install it again.

hope that helps.

lg

fastrde

I think your are adding plugin manually. use the command:(strictly if you are using CLI build)

phonegap local plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-geolocation.git

I have the same problem with you, but finally I have solved it.

The reason to occur this problem is we always to use browser's navigator.geolocation.getCurrentPosition function to locate the postion,so we must let app use cordova native implementation instead of html5.

peter_the_oak

In this thread are a lot of good answers, thx to everybody. I would like to add my solution in case sb encounters the same.

When I launched my Cordova 3.5 app on my Nexus, the GPS service didn't start after this line:

navigator.geolocation.watchPosition(...);

Instead, I got this message:

07-02 14:31:09.932: D/PluginManager(27767): exec() call to unknown plugin: Geolocation

I was confused because there was no Java Geolocation.java file at all despite of a correct installation of the plugin.

When it got clear by the answer of Natsu that the Cordova team decided to invoke the native browser object, I commented out the Geolocation plugin in the cordova_plugins.js:

...
{
    "file": "plugins/org.apache.cordova.geolocation/www/Position.js",
    "id": "org.apache.cordova.geolocation.Position",
    "clobbers": [
        "Position"
    ]
} 
/*----
,
{
    "file": "plugins/org.apache.cordova.geolocation/www/geolocation.js",
    "id": "org.apache.cordova.geolocation.geolocation",
    "clobbers": [
        "navigator.geolocation"
    ]
}  
----*/
,
{
    "file": "plugins/org.apache.cordova.splashscreen/www/splashscreen.js",
    "id": "org.apache.cordova.splashscreen.SplashScreen",
    "clobbers": [
        "navigator.splashscreen"
    ]
},
...

The rest of the plugin installation remained unchanged. Like this, the original Chrome object doesn't get clobbered and everything works fine.

I am now a bit unsure why the plugin installer adds the JSON entry although it will lead to malfunction. I guess it's just a little design weakness that can't be corrected right now (...or I should dig much deeper into plugin installation).

EDIT: Not to forget to uncomment the metadata:

// "org.apache.cordova.geolocation": "0.3.8",
...

If the error code was 3, verify if the location in settings are enabled. This is work for me.

adding the below permission in manifest file solved my issue

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

There are issues with cordova-plugin-geolocation latest verions. Try below version

<plugin name="cordova-plugin-geolocation" spec="2.4.3" />
OmmyJay

Had the same issue with https://github.com/apache/cordova-plugin-geolocation. Solved it by changing this configuration

{ enableHighAccuracy: true }

on getCurrentPosition() and turning 'on' the gps for genymotion.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!