public function FreeGeoIp::getCoordinates in Geolocation Field 8.3
Same name and namespace in other branches
- 8.2 src/Plugin/geolocation/Location/FreeGeoIp.php \Drupal\geolocation\Plugin\geolocation\Location\FreeGeoIp::getCoordinates()
Get map location.
Parameters
int $location_option_id: Location option ID.
array $location_option_settings: The current feature settings.
mixed $context: Context like field formatter, field widget or view.
Return value
array With content 'lat' => latitude 'lng' => longitude
Overrides LocationBase::getCoordinates
File
- src/
Plugin/ geolocation/ Location/ FreeGeoIp.php, line 22
Class
- FreeGeoIp
- Fixed coordinates map center.
Namespace
Drupal\geolocation\Plugin\geolocation\LocationCode
public function getCoordinates($center_option_id, array $center_option_settings, $context = NULL) {
$ip = \Drupal::request()
->getClientIp();
if (empty($ip)) {
return [];
}
$json = file_get_contents("http://freegeoip.live/json/" . $ip);
if (empty($json)) {
return [];
}
$result = json_decode($json, TRUE);
if (empty($result) || empty($result['latitude']) || empty($result['longitude'])) {
return [];
}
return [
'lat' => (double) $result['latitude'],
'lng' => (double) $result['longitude'],
];
}