1

I have succeeded in getting the Latitude and longitude of my current location. Then I need to use the lat and long to get the physical address of that particular location. later retrieve the physical address from the response and try to print on input fields. Can anyone help me with that?

handleClick(){
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(position => {

            // Get the Latitude and Longitude from Geolocation API
             this.latitude = position.coords.latitude;
            console.log('@@@Latitude', this.latitude);
            this.longitude = position.coords.longitude;
            console.log('@@@Longitude', this.longitude);
           
            
            // Add Latitude and Longitude to the markers list.
            this.lstMarkers = [{
                location : {
                    Latitude: this.latitude,
                    Longitude : this.longitude,
                   
                },
                title : 'You are here'
            }];
            this.zoomlevel = "4";
        });
doItAll({lat: this.latitude ,longi: this.longitude })
    .then ((result)=>{   
    console.log('hi');
    console.log('a',this.latitude);
    console.log('b',this.longitude);  
    this.doItAll = result;
    console.log('result',result);
    
     
})

**From the above code I am able to get the Lat and long. ** later this latitude and longitude will be used dynamically in the Apex class to get the physical address.

public class VF_CurrentLocation {
 @AuraEnabled
public static List<Object> doItAll(double lat,double longi) {        
    Http h = new Http();
HttpRequest req = new HttpRequest();
    req.setMethod('GET');       
    req.setEndpoint('https://maps.googleapis.com/maps/api/geocode/json?latlng='+lat+','+longi                           +'&key=AIzaSyDqYToRPOG1TBKujKZyhmF00BXgR-4-0Zo');
HttpResponse res = h.send(req);

     
    Map<String,Object> gr = (Map<String,Object>)JSON.deserializeUntyped(res.getBody());
    List<Object> results = (List<Object>)gr.get('results');
    Map<String,Object> firstResult = (Map<String,Object>)results[0]; // just taking the first one for this example
          
   
    
List<Object> firstAddressComponents = (List<Object>)firstResult.get('address_components'); 
    system.debug('address'+firstAddressComponents);
    return firstAddressComponents;
   
}

}

** this is the apex class which I am using**

enter image description here

I am now able to get the physical address. However don't know how to get the city, state , country, postal code of that location individually. Like if I want to show the address detail of that location in different text field.

2
  • Have you tried to send these coordinates to the Google API? Could you include this code into your question? Generally this appears to me not so much a Salesforce question as a Google API question, which you could post over at Stackoverflow. May 4, 2022 at 7:22
  • 1
    @FelixvanHove I have updated the question. can You help me with this.
    – Anish
    May 4, 2022 at 9:15

0

You must log in to answer this question.

Browse other questions tagged .