FreeGeoIp.php in Geolocation Field 8.3
File
src/Plugin/geolocation/Location/FreeGeoIp.php
View source
<?php
namespace Drupal\geolocation\Plugin\geolocation\Location;
use Drupal\geolocation\LocationInterface;
use Drupal\geolocation\LocationBase;
class FreeGeoIp extends LocationBase implements LocationInterface {
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'],
];
}
}