function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
BB 

get current latitude and longitude javascript function ?

I have the map 
var lat, lng
var map = component.get('v.map'); 

map.locate({setView: true,  maxZoom: 13});  
               function onLocationFound(e) {
                      lat = e.latlng.lat
                      lng = e.latlng.lng
               }
I need specifficaly lat lng from my current position
Best Answer chosen by B
Raj VakatiRaj Vakati
Refer this code
 
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >

     <!-- 1.handler to call method "doInit" of jsCOntroller on loading of component. -->

    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />

    Latitude :

    <div id="startLat">

    </div>

    Longitude :

    <div id="startLon">

    </div>

</aura:component>
 
({

    //1. Method called on loading of component.

 doInit : function(component, event, helper) {

        //2. Declearing variable to show Lat&Long.

  var startPos;

        var geoSuccess = function(position) {

            startPos = position;

            //3.Setting Lat in component.

            document.getElementById('startLat').innerHTML = startPos.coords.latitude;

            //4.Setting Long in component.

            document.getElementById('startLon').innerHTML = startPos.coords.longitude;

        };

        navigator.geolocation.getCurrentPosition(geoSuccess);

 }

})

 

All Answers

Raj VakatiRaj Vakati
Refer this code
 
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >

     <!-- 1.handler to call method "doInit" of jsCOntroller on loading of component. -->

    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />

    Latitude :

    <div id="startLat">

    </div>

    Longitude :

    <div id="startLon">

    </div>

</aura:component>
 
({

    //1. Method called on loading of component.

 doInit : function(component, event, helper) {

        //2. Declearing variable to show Lat&Long.

  var startPos;

        var geoSuccess = function(position) {

            startPos = position;

            //3.Setting Lat in component.

            document.getElementById('startLat').innerHTML = startPos.coords.latitude;

            //4.Setting Long in component.

            document.getElementById('startLon').innerHTML = startPos.coords.longitude;

        };

        navigator.geolocation.getCurrentPosition(geoSuccess);

 }

})

 
This was selected as the best answer
BB
var geoSuccess = function(position) {
             startPos = position;   -- this is shown as undefinied  

            //3.Setting Lat in component.
             component.set('v.startLat',startPos.coords.latitude);
            //4.Setting Long in component.
            component.set('v.startLng',startPos.coords.longitude);
        };
alert(component.get('v.startLat'));  - null

The component declaration of Lng and Lat
<aura:attribute name="startLng" type="Double"/>
    <aura:attribute name="startLat" type="Double"/>

I just need lat and long for using them somewhere else 
Any ideas ? 
BB
Finally based on what you gave i did it like this 

map.locate({setView: true,  maxZoom: 13});  
               function onLocationFound(e) {
               L.marker(e.latlng,{icon:pin_green}).addTo(map)
                .bindPopup("You are here").openPopup();
               component.set('v.startLat',e.latitude);
               component.set('v.startLng',e.longitude);
               }
And i set it in the function where the map is loaded beacuse i tried to get lat and long it in second function and it just cant be done like that

Thanks for help