Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Cordova plugin Geolocation - Plugin provides the information about device's location

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 815
    Comment on it

    Hello Readers,


    Cordova geolocation API can access the GPS data and provides the information about device's location in the form of its longitude and latitude. GPS, IP address, WIFI, Bluetooth MAC address, GCM/CDMA cell's IDs these all are the common source of information from which this API get the information. But these information can be wrong as this API can provide wrong location of device.


    First of all we need to install the plugin through CLI:


    For Cordova 5.0+ version:

     

    $ cordova plugin add cordova-plugin-geolocation


    For Cordova older version:

     

    $ cordova plugin add org.apache.cordova.geolocation


    Or install via repo url directly:

     

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

     


    This plugin creates a global object navigator.geolocation and following are the platforms supported:

    • Amazon Fire OS
    • Android
    • BlackBerry 10
    • Firefox OS
    • iOS
    • Tizen
    • Windows Phone 7 and 8
    • Windows

     

    Geolocation plugin provides some methods. Now lets have a look on these methods and their examples:

     

    • navigator.geolocation.getCurrentPosition: This method returns the current position of the device.

     

    For Example:

     

    navigator.geolocation.getCurrentPosition(onSuccess, onError);
    function onSuccess(position) {
        var lat, long;
        lat = position.coords.latitude;
        long = position.coords.longitude;              
    }  
    // Show an alert if there is a problem getting the geolocation
    function onError(error) {
        console.log(error.code);
    }   



    In the above example, we are using getCurrentPosition() method. This method returns the device's current location and with two parameters i.e (success callback, error callback). The onSuccess callback is passing a position object which is containing device's current position and the onError callback in passing an error object as parameter. When any error occurs onError callback will execute.

     

    • navigator.geolocation.watchPosition: watchPosition() method returns the device's location if user changes the device position i.e watchPosition() method returns the device's location when user updates the device's position.

     

    For Example:

     

    function onSuccess(position) {
        var lat, long;
        lat = position.coords.latitude;
        long = position.coords.longitude;              
    }  
    // Show an alert if there is a problem getting the geolocation
    function onError(error) {
    
        console.log(error.code);
    }   
    navigator.geolocation.watchPosition(onSuccess, onError, { timeout: 30000 });

     

    In the above example, the watchPosition() method checks in every 30 sec if there is any change in device position. Then we can get new position in onSuccess callback method.

     

    • navigator.geolocation.clearWatch: This method is used to clear the watchPosition.

     

    For Example:

     

    var watchID = navigator.geolocation.watchPosition(onSuccess, onError, { timeout: 30000 });
    navigator.geolocation.clearWatch(watchID);

     

    The position object contains following properties:

     

    • latitude: The value is in decimal numbers.
    • longitude: The value in decimal numbers.
    • altitude: Height of the position in meters.
    • accuracy: Accurate value of the latitude and longitude coordinates in meters.
    • altitudeAccuracy: Accurate value of the altitude coordinate in meters.
    • heading: Travel direction in degrees counting clockwise.
    • speed: Speed of device in meters per second.

     

    Here is the use and purpose of Geolocation API in Cordova / PhoneGap apps. For more details CLICK HERE.

    Hope this helps :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: