i am trying to build a lightning component that shows nearby contacts based on the users current location. i am getting an error when trying to access the users current location through navigator.geoLocation. I get the following error. "Uncaught error in actionCallback : navigator.geoLocation is undefined"

Here is my component and its controller logic. Component:

<aura:component >        
   <aura:attribute name="latitude" type="Decimal"/>
    <aura:attribute name="longitude" type="Decimal"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />      

Controller Logic:


    doInit : function(component, event, helper) {
            console.log("capability is there");        


            console.log("No Capability");


            this.longitude=component.set("v.longitude", position.coords.longitude);
            console.log("Latitude is:"+this.latitude);
            console.log("Longitude is:" +this.longitude);




debugging console log shows that my else part is only written to console. does that mean that geolocation is not supported in lightning? Am i doing anything wrong ? Any help would be appreciated.

Thanks in advance.

  • If you can't tap into the devices native geolocation, which usually requires the user's permission (presumably running within SF1 mobile you'd have it), you're not going to be able to use geolocation. You might want to look at the SF1 Mobile App Dev Guide for some hints on accessing it.
    – crmprogdev
    Commented Aug 25, 2015 at 19:14
  • looks like this is a duplicate of this Commented Jan 30, 2019 at 14:30

3 Answers 3


Just a thought. There are many services (like http://ip-api.com) available from where you can get the geolocation by calling their APIs.

You can try to call this api via javascript at front end and pass the geolocation to the controller. The JavaScript is as below. Also, I have tested it in JsFiddle (http://jsfiddle.net/5zokh6zx/) and it is giving a proper result. Hope this helps.

function myGeoLocation() {
    if (window.XMLHttpRequest) xmlhttp = new XMLHttpRequest();
    else xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");


    var obj = JSON.parse(xmlhttp.responseText);

    return obj;

I think you want to use geolocation, not geoLocation.

You get the error because you test if it's null but then use it anyway. You should probably move the call to getCurrentPosition to be inside the success part of the if().

edit: oops.. i'm several years late to this party.. hope you got it sorted before this.


I tried run you code and i see the error.

please use the code below:

<aura:attribute name="latitude" type="decimal" default=""/>
<aura:attribute name="longitude" type="decimal" default=""/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}" /> 

doInit : function(component, event, helper) {
            console.log("capability is there");
            console.log("No Capability");
        navigator.geolocation.getCurrentPosition(function(position) {
            var latit = position.coords.latitude;
            var longit = position.coords.longitude;
            console.log("The Latitude is:"+ latit);
            console.log("The Latitude is:" +longit);            

the see de debug in the console log.

See you there!!!

  • 2
    Hi Jpeixoto, welcome to SFSE. Could you expand your answer by explaining what you changed and why? "Code dump" answers that don't provide explanation offer less value to the asker and the community.
    – David Reed
    Commented Jan 30, 2019 at 14:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .