Search code examples
ibm-mobilefirst

IBM MobileFirst -- get location


I have MobileFirst (V7.O) app . How do I get the current latitude/Longitude for the device. Basically, I am recording some information at various point in my app and user can be moving.. So, at various instance, I would like to get the current lat/log..

Can you please provide some hints and how to get this information

Thanks for your help


Solution

  • Cordova is bundled in your MobileFirst project. As such you can take use of Cordova APIs to achieve this.

    See the following (scroll down to the API usage): https://github.com/apache/cordova-plugin-geolocation

    // onSuccess Callback
    // This method accepts a Position object, which contains the
    // current GPS coordinates
    //
    var onSuccess = function(position) {
        alert('Latitude: '          + position.coords.latitude          + '\n' +
              'Longitude: '         + position.coords.longitude         + '\n' +
              'Altitude: '          + position.coords.altitude          + '\n' +
              'Accuracy: '          + position.coords.accuracy          + '\n' +
              'Altitude Accuracy: ' + position.coords.altitudeAccuracy  + '\n' +
              'Heading: '           + position.coords.heading           + '\n' +
              'Speed: '             + position.coords.speed             + '\n' +
              'Timestamp: '         + position.timestamp                + '\n');
    };
    
    // onError Callback receives a PositionError object
    //
    function onError(error) {
        alert('code: '    + error.code    + '\n' +
              'message: ' + error.message + '\n');
    }
    
    navigator.geolocation.getCurrentPosition(onSuccess, onError);