Skip to content

Instantly share code, notes, and snippets.

@LokeshSagi
Created March 28, 2020 13:42
Show Gist options
  • Save LokeshSagi/d8f35f6fe67117e1939fe9e0742c0245 to your computer and use it in GitHub Desktop.
Save LokeshSagi/d8f35f6fe67117e1939fe9e0742c0245 to your computer and use it in GitHub Desktop.
public class GoogleAPI {
//Generic method to get GoogleAPIKey from Custom Label.
public static String getAPIKey() {
String api = Label.GoogleAPIKey;
return api;
}
//Generic Http Callout Method
public static HttpResponse getResponse(string strURL){
Http h = new Http();
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
req.setMethod('GET');
req.setEndpoint(strURL);
req.setTimeout(120000);
res = h.send(req);
return res;
}
@AuraEnabled(cacheable=true)
public static string getAddressFromLatLong(String lat, String lon){
System.debug('Incoming - '+lat+' '+lon);
String result = null;
try {
String latlong = lat + ',' + lon;
String apiURL = 'https://maps.googleapis.com/maps/api/geocode/json?latlng=' + latlong + '&key=' +
getAPIKey();
system.debug('apiURL is1 ' + apiURL);
HttpResponse res = getResponse(apiURL);
if(res.getStatusCode() == 200) {
system.debug('API invoked successfully');
result = res.getBody();
}
} catch (Exception e) {
throw new AuraHandledException('Error while getting current location ---> '+e.getMessage());
}
return result;
}
}
<template>
<template if:true={showSpinner}>
<div class="backdrop">
<div class="spinner">
<lightning-spinner alternative-text="Loading...">
</lightning-spinner>
</div>
</div>
</template>
<div class="slds-grid slds-wrap">
<div
class="slds-col slds-size_1-of-1 slds-medium-size_12-of-12 slds-large-size_12-of-12 slds-align_absolute-center">
<button class="slds-button slds-button_brand" onclick={handleCurrentLocation}>Use my current
location</button>
</div>
</div>
</template>
import { LightningElement, track } from 'lwc';
import myCurrentDetails from '@salesforce/apex/Perqs_GoogleLocationSearchAPI.getAddressFromLatLong';
export default class GoogleGeoCodingAPI extends LightningElement {
@track showSpinner = false;
@track lat;
@track long;
@track address;
@track selectedBusinessLabel;
handleCurrentLocation() {
navigator.geolocation.getCurrentPosition(success => {
this.showSpinner = true;
this.lat = success.coords.latitude;
this.long = success.coords.longitude;
console.log('lat -> ' + this.lat);
console.log('long -> ' + this.long);
if (this.lat && this.long) {
myCurrentDetails({ lat: this.lat, lon: this.long })
.then(result => {
let response = JSON.parse(result).results[0];
console.log('response - ' + JSON.stringify(response));
let postalCode = '', premise = '', state = '', stateCode = '', country = '', countryCode = '', city = '', street = '', street_number = '', route = '', subLocal1 = '', subLocal2 = '', subLocal3 = '';
if (response.address_components.length > 0) {
for (let key in response.address_components) {
let fieldLabels = response.address_components[key].types;
fieldLabels.forEach(fieldLabel => {
if (fieldLabel === 'sublocality_level_3' || fieldLabel === 'sublocality_level_2' || fieldLabel === 'sublocality_level_1' || fieldLabel === 'street_number' ||
fieldLabel === 'route' || fieldLabel === 'locality' || fieldLabel === 'country' || fieldLabel === 'postal_code' ||
fieldLabel === 'administrative_area_level_1') {
switch (fieldLabel) {
case 'premise':
premise = response.address_components[key].long_name;
break;
case 'sublocality_level_3':
subLocal3 = response.address_components[key].long_name;
break;
case 'sublocality_level_2':
subLocal2 = response.address_components[key].long_name;
break;
case 'sublocality_level_1':
subLocal1 = response.address_components[key].long_name;
break;
case 'street_number':
street_number = response.address_components[key].long_name;
break;
case 'route':
route = response.address_components[key].short_name;
break;
case 'postal_code':
postalCode = response.address_components[key].long_name;
break;
case 'administrative_area_level_1':
state = response.address_components[key].long_name;
stateCode = response.address_components[key].short_name;
break;
case 'country':
country = response.address_components[key].long_name;
countryCode = response.address_components[key].short_name;
break;
case 'locality':
city = response.address_components[key].long_name;
break;
default:
break;
}
}
});
}
console.log('subLocal3 - ' + subLocal3);
console.log('subLocal2 - ' + subLocal2);
console.log('subLocal1 - ' + subLocal1);
console.log('street_number - ' + street_number);
console.log('route - ' + route);
if (street_number && route) {
street = street_number + ' ' + route;
}
else {
street = street_number + ' ' + route;
}
console.log('street - ' + street + street.length);
if (street.trim().length === 0) {
let val = '';
if (premise) {
val = premise;
}
if (subLocal3) {
val = val + ', ' + subLocal3;
}
console.log('val - ' + val);
street = val;
}
}
// console.log('street --> ' + street);
// console.log('city --> ' + city);
// console.log('state --> ' + state);
// console.log('country --> ' + country);
// console.log('postalCode --> ' + postalCode);
let address = {
street: street,
city: city,
state: state,
stateCode: stateCode,
country: country,
countryCode: countryCode,
postalCode: postalCode
};
this.address = address;
console.log('address - ' + JSON.stringify(address));
this.selectedBusinessLabel = response.formatted_address;
this.showSpinner = false;
})
.catch(error => {
this.showSpinner = false;
console.log('Error while getting current location details - ' + JSON.stringify(error));
});
} else {
this.showSpinner = false;
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment