leaflet + phonegap = Geolocation error: timeout expired

1,331 views
Skip to first unread message

P. Zaccaria

unread,
Jul 10, 2014, 12:29:55 PM7/10/14
to leafl...@googlegroups.com
Hi,
I have a leaflet based map with geolocation which is working when using it on a webpage.

If I try to make an Android app from it using phonegap, the geolocation never works:
I always get the error "Geolocation error: timeout expired"

Geolocation in the index.html is coded this way:

function onLocationFound(e) {
    var radius = e.accuracy / 2;

    L.marker(e.latlng).addTo(map)
        .bindPopup("You are within " + radius + " meters from this point").openPopup();

    L.circle(e.latlng, radius).addTo(map);
}

function onLocationError(e) {
    alert(e.message);
}

function toggle_gps() {

    if (!gps) {
        map.locate({setView: true, maxZoom: 16, timeout: 600000});
    } else {
        map.stopLocate();
    }
    gps = 1-gps;
}

In the phonegap/cordova config.xml I have:

    <feature name="Geolocation">
        <param name="android-package" value="org.apache.cordova.GeoBroker" />
    </feature>

Any idea why it doesn't work? Suggestions on how to better debug this error?

P. Zaccaria

unread,
Jul 10, 2014, 5:13:11 PM7/10/14
to leafl...@googlegroups.com
Found the solution:

in the phonegap\MYPROJECT\plugins one must install org.apache.cordova.geolocation and create an appropriate android.json file:

{
    "prepare_queue": {
        "installed": [],
        "uninstalled": []
    },
    "config_munge": {
        "files": {
            "AndroidManifest.xml": {
                "parents": {
                    "/*": [
                        {
                            "xml": "<uses-permission android:name=\"android.permission.ACCESS_COARSE_LOCATION\" />",
                            "count": 1
                        },
                        {
                            "xml": "<uses-permission android:name=\"android.permission.ACCESS_FINE_LOCATION\" />",
                            "count": 1
                        }
                    ]
                }
            }
        }
    },
    "installed_plugins": {
        "org.apache.cordova.geolocation": {
            "PACKAGE_NAME": "com.example.project" <--- replace this with your package name
        }
    },
    "dependent_plugins": {}
}
Reply all
Reply to author
Forward
0 new messages