Using Google Maps, I was trying to fetch the current city and other details in a Lightning component using Javascript, but I was getting some error in browser console. I believe that is because of Locker service as described in the reference link. So I had to take a different approach.
But first, we need the latitude and longitude of the user who is accessing the Lightning page. The HTML Geolocation API is used to get the geographical position of a user. Since this can compromise privacy, the position is not available unless the user approves it. The
To fetch the city details, we need to invoke the Google maps API. We need to send the location details as parameters. The
Please go through the documentation of Google maps, the link of which has been provided in the reference section. The following fields of the results element are of significance:
1.
2.
And that's it.
Reference: http://www.rattanpal.com/2017/03/19/salesforce-implement-google-maps-lightning-components/
https://developers.google.com/maps/documentation/geocoding/intro
But first, we need the latitude and longitude of the user who is accessing the Lightning page. The HTML Geolocation API is used to get the geographical position of a user. Since this can compromise privacy, the position is not available unless the user approves it. The
getCurrentPosition()
method is used to return the user's position. The example below returns the latitude and longitude of the user's position.checkGeoLocation : function(component, event, helper) {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var lat = position.coords.latitude;
var lon = position.coords.longitude;
var action = component.get("c.getCityName");
action.setParams({
"latitude": lat,
"longitude": lon
});
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
var location = response.getReturnValue();
}
});
$A.enqueueAction(action);
});
} else {
console.log('Your browser does not support GeoLocation');
}
}
To fetch the city details, we need to invoke the Google maps API. We need to send the location details as parameters. The
results
element in the response needs to be parsed in Apex to extract the desired values. In my case, JsonReader
class did the work for me.Please go through the documentation of Google maps, the link of which has been provided in the reference section. The following fields of the results element are of significance:
1.
address_components[]
2.
types[]
@AuraEnabled
public static String getCityName (Decimal latitude, Decimal longitude) {
String url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=' + latitude + ',' + longitude + '&sensor=true';
JsonReader jsonobj = new JsonReader(getHttp(url));
String status = jsonObj.getString('status');
System.debug(status);
JsonReader addressComponentObj = null;
String locality= null;
String adminAreaLevel2= null;
List<Object> types = null;
for(Object obj : jsonObj.getList('results[0].address_components')) {
addressComponentObj = new JsonReader(obj);
types = addressComponentObj.getList('types');
for(Object typeObj : types) {
String type = (String) typeObj;
if (type.equalsIgnoreCase('locality')) {
locality = addressComponentObj.getString('long_name');
}
if (type.equalsIgnoreCase('administrative_area_level_2')) {
adminAreaLevel2 = addressComponentObj.getString('short_name');
}
}
}
return (locality + ', ' + adminAreaLevel2);
}
And that's it.
Reference: http://www.rattanpal.com/2017/03/19/salesforce-implement-google-maps-lightning-components/
https://developers.google.com/maps/documentation/geocoding/intro