Exemple #1
0
 public static function geocode($address = NULL)
 {
     if ($address) {
         $coordinates = self::checkAndGetCoordinates($address);
         if ($coordinates) {
             $geocodes = array('country' => '', 'location_name' => '', 'latitude' => $coordinates['lat'], 'longitude' => $coordinates['lng']);
             return $geocodes;
         }
         $map_object = new GoogleMapAPI();
         $map_object->setLookupService("GOOGLE");
         $geocodes_full = $map_object->geoGetCoordsFull($address);
         // Verify that the request succeeded
         if (!isset($geocodes_full->status)) {
             return FALSE;
         }
         if ($geocodes_full->status != 'OK') {
             return FALSE;
         }
         // Convert the Geocoder's results to an array
         $all_components = json_decode(json_encode($geocodes_full->results), TRUE);
         $location = $all_components[0]['geometry']['location'];
         // Find the country
         $address_components = $all_components[0]['address_components'];
         $country_name = NULL;
         foreach ($address_components as $component) {
             if (in_array('country', $component['types'])) {
                 $country_name = $component['long_name'];
                 break;
             }
         }
         // If no country has been found, use the formatted address
         if (empty($country_name)) {
             $country_name = $all_components[0]['formatted_address'];
         }
         $geocodes = array('country' => $country_name, 'location_name' => $all_components[0]['formatted_address'], 'latitude' => $location['lat'], 'longitude' => $location['lng']);
         return $geocodes;
     } else {
         return FALSE;
     }
 }