You are here

FreeGeoIp.php in Geolocation Field 8.3

Same filename and directory in other branches
  1. 8.2 src/Plugin/geolocation/Location/FreeGeoIp.php

File

src/Plugin/geolocation/Location/FreeGeoIp.php
View source
<?php

namespace Drupal\geolocation\Plugin\geolocation\Location;

use Drupal\geolocation\LocationInterface;
use Drupal\geolocation\LocationBase;

/**
 * Fixed coordinates map center.
 *
 * @Location(
 *   id = "freeogeoip",
 *   name = @Translation("freegoip.live Service"),
 *   description = @Translation("See http://freegeoip.live website. Limited to 15000 requests per hour."),
 * )
 */
class FreeGeoIp extends LocationBase implements LocationInterface {

  /**
   * {@inheritdoc}
   */
  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'],
    ];
  }

}

Classes

Namesort descending Description
FreeGeoIp Fixed coordinates map center.