You are here

public function AddressToGeo::geocode in Geolocation Address Link 8

Geocode an address.

Note that the returned address may be cleaned up and expanded by the address formatter and the geocoding service.

Parameters

mixed address: Either a string address or an associative array using the architecture provided by the Address module. @see \Drupal\address\Element\Address::applyDefaults().

Return value

array 'lat': string LATITUDE 'long': string LONGITUDE 'data': array Map settings

File

src/AddressToGeo.php, line 200

Class

AddressToGeo
Class AddressToGeo.

Namespace

Drupal\geolocation_address_link

Code

public function geocode($address, $map_size = '400x400') {
  if (is_array($address)) {
    $address = $this
      ->addressArrayToString($address);
  }
  $address = str_replace(' ', '+', $address);
  if ($result = $this->geocoder
    ->geocode($address)) {

    // Store the boundary and address data returned by the geocoding service,
    // and use the boundary to compute a logical zoom setting for this specific
    // location.
    return [
      'lat' => $result['location']['lat'],
      'lng' => $result['location']['lng'],
      'lat_sin' => sin(deg2rad($result['location']['lat'])),
      'lat_cos' => cos(deg2rad($result['location']['lat'])),
      'lng_rad' => deg2rad($result['location']['lng']),
      'data' => [
        'boundary' => $result['boundary'],
        'address' => $result['address'],
        'zoom' => $this
          ->getZoom($result['boundary'], $map_size),
      ],
    ];
  }
  return FALSE;
}