简体   繁体   中英

Phonegap GPS location plugin cordova-plugin-gpslocation

I am trying to get gps coordinates to move a google map in my phone. My xml file has the plugin added like this:

<plugin name="cordova-plugin-gpslocation" spec="1" />

When I click my button to get my coordinates this code runs:

 function locateMe(){
        alert("in function");

    function onSuccess(position) {
        alert(position.coords.latitude);
        alert(position.coords.longitude);

    function onError(error) {
        alert('code: '    + error.code    + '\n' +
              'message: ' + error.message + '\n');
        }
    //locateMe code
    var watchID = GPSLocation.getCurrentPosition(onSuccess, onError);


    //get gps coordinate


    //move map to location   

    }


}

The gps Plugin I am in using: https://www.npmjs.com/package/cordova-plugin-gpslocation

My app seems to recognize the plugin is added because when I install it on my android phone it warns me about the gps permissions.

When I run the above code I only get the first alert for in fucntion and I am not sure I am not getting the alerts for the gps coordinates.

Try using official Cordova plugin Visit: https://cordova.apache.org/docs/en/latest/guide/cli/index.html

Installation: This requires cordova 5.0+ ( current stable 1.0.0 )

cordova plugin add cordova-plugin-geolocation

Older versions of Cordova can still install via the deprecated id ( stale 0.3.12 )

cordova plugin add org.apache.cordova.geolocation

It is also possible to install via repo url directly ( unstable )

cordova plugin add https://github.com/apache/cordova-plugin-geolocation.git

To get the device current location on button click, you can call / invoke below sample code on button click

function getWeatherLocation() {
   navigator.geolocation.getCurrentPosition(onWeatherSuccess, onWeatherError,{enableHighAccuracy: true });
}

// Success callback for get geo coordinates

var onWeatherSuccess = function (position) {

    Latitude = position.coords.latitude;
    Longitude = position.coords.longitude;

    //Do something with coordinates
}

// Error callback

function onWeatherError(error) {
    console.log('code: ' + error.code + '\n' +
        'message: ' + error.message + '\n');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM