You are here

function ip_geoloc_flatten_google_address in IP Geolocation Views & Maps 8

Same name and namespace in other branches
  1. 7 ip_geoloc.module \ip_geoloc_flatten_google_address()

Fleshes out the $ip_geoloc_address array.

This is based on the additional data provided in the $google_address array. This may involve tweaking of the 'latitude' and 'longitude' entries so that they remain consistent with the street address components.

Parameters

array $google_address: Array of address components as returned by Google service.

array $ip_geoloc_address: The $google_address in flattened form.

Return value

bool TRUE, unless google_address or ip_geoloc_address are empty

2 calls to ip_geoloc_flatten_google_address()
IpGeoLocAPI::getLocationByIp in src/Services/IpGeoLocAPI.php
Returns the location details associated with the supplied IP address.
ip_geoloc_get_location_by_ip in ./ip_geoloc_api.inc
Returns the location details associated with the supplied IP address.

File

./ip_geoloc.module, line 512
IPGV&M is a mapping engine for Views that contain locations of entities and/or visitors. Google Maps, Leaflet and OpenLayers2 maps are all supported and available through this module. Using a number of optional sources IPGV&M also retrieves…

Code

function ip_geoloc_flatten_google_address(array $google_address, array &$ip_geoloc_address) {
  if (is_array($google_address) && is_array($google_address['address_components']) && is_array($ip_geoloc_address)) {
    $ip_geoloc_address['provider'] = 'google';
    foreach ($google_address['address_components'] as $component) {
      $long_name = $component['long_name'];
      if (!empty($long_name)) {
        $type = $component['types'][0];
        $ip_geoloc_address[$type] = $long_name;
        if ($type == 'country' && !empty($component['short_name'])) {
          $ip_geoloc_address['country_code'] = $component['short_name'];
        }
      }
    }
    $ip_geoloc_address['formatted_address'] = $google_address['formatted_address'];

    // The following may be slightly different from the original lat,long passed
    // into ip_geoloc_reverse_geocode().
    $ip_geoloc_address['latitude'] = $google_address['geometry']['location']['lat'];
    $ip_geoloc_address['longitude'] = $google_address['geometry']['location']['lng'];
    return TRUE;
  }
  return FALSE;
}