Skip to content

Instantly share code, notes, and snippets.

@isramv
Created May 2, 2019 20:59
Show Gist options
  • Save isramv/aac21c8928adf49900ed78cb859e8c81 to your computer and use it in GitHub Desktop.
Save isramv/aac21c8928adf49900ed78cb859e8c81 to your computer and use it in GitHub Desktop.

Drupal\geolocation\Plugin\Field\FieldType\GeolocationItem.php

public static function generateSampleValue(FieldDefinitionInterface $field_definition) {

  // Guadalajara.
  $longitude = (float) -103.3496;
  $latitude = (float) 20.6597;
  $radius = rand(1,5); // in miles

  $lng_min = $longitude - $radius / abs(cos(deg2rad($latitude)) * 69);
  $lng_max = $longitude + $radius / abs(cos(deg2rad($latitude)) * 69);
  $lat_min = $latitude - ($radius / 69);
  $lat_max = $latitude + ($radius / 69);

  $values['lat'] = rand($lat_min, $lat_max);
  $values['lng'] = rand($lng_min, $lng_max);
  return $values;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment