1

A simple code getting current user geolocation is strangely ... ignored: it doesn't go to the success callback, neither in the error callBack, so I really have no idea of the cause. As window.geolocation is provided in LockerService secureWindow, I assume it's supposed to work ... but how ?

Many thanks & best regards

Sample code to reproduce:

 getCurrentPositionHlp : function(component,event) {
    debugger;
    if (window.navigator.geolocation) {
        var GeolocOptions = {
            enableHighAccuracy: true,
            timeout: 5000,
            maximumAge: 0
        };            
        var selfPos45 = this ;
        window.navigator.geolocation.getCurrentPosition(
            function(position){
                debugger;
                var lat = position.coords.latitude;
                var lon = position.coords.longitude;
                component.set('v.currentLatitude',lat);
                component.set('v.currentLongitude',lon);
                selfPos45.logHlp('info','GEOLOC: Set current position to '+lat+' - '+lon+ '(accuracy: ' + acc + ' meters)');
            } ,
            function(err){
                debugger;
                selfPos45.lohHlp('warning','GEOLOC ERROR:(' + err.code + '): ' + err.message);
            },
            GeolocOptions);
    }
    else
    {
        this.logHlp('warning','GEOLOC: Unable to get navigator current position')
    }       
}, 
3

We use navigator.geolocation.getCurrentPosition() in a Lightning for Communities app with LockerService enabled.

Here's some sample code you can use to prove that it works within LockerService. Set up an init handler in your component, then add this to your controller.

// asks the user to get the location of their browser
doInit: function(component, event, helper) {
    navigator.geolocation.getCurrentPosition(function(e) {
        console.log(e.coords.latitude + ', ' + e.coords.longitude);
    }, function() {
        console.log('There was an error.');
    });
}

Refresh the page that holds this component with the dev console open and you'll see your lat/long.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.