Geolocation provides latitude and longitude information. In order to search based on these values, when zipcode or city name is passed to this function, data is returned comma separated.
$lat_lan = module_name_get_latitue_longitude(‘delhi’);
$lat_lan = module_name_get_latitue_longitude(‘110001’);
/**
* Get Latitude longitude.
*
* @param type $zip
* Zipcode
*
* @return string
* Returns latitude and longitude, if found.
*/
function module_name_get_latitue_longitude($zip) {
$geolocation = \Drupal::service('geolocation.core');
$geocoderManager = $geolocation->getGeocoderManager();
$geocoder = $geocoderManager->getGeocoder('google_geocoding_api');
$geocode = $geocoder->geocode($zip);
$lat_lng = '';
if (isset($geocode['location'])) {
$lat_lng = $geocode['location']['lat'] . ',' . $geocode['location']['lng'];
}
return $lat_lng;
}