function location_latlon_rough_default in Location 7.4
Same name and namespace in other branches
- 5.3 location.inc \location_latlon_rough_default()
- 6.3 location.inc \location_latlon_rough_default()
- 7.5 location.inc \location_latlon_rough_default()
- 7.3 location.inc \location_latlon_rough_default()
Returns a lat/lon pair of the approximate center of the given postal code in the given country This function is a default implementation, in case the country support doesn't implement a proper function for this.
Parameters
$location: An associative array $location where 'street' => the street portion of the location 'supplemental' => additional street portion of the location 'province' => the province, state, or territory 'country' => lower-cased two-letter ISO code (REQUIRED) 'postal_code' => the international postal code for this location (REQUIRED)
Return value
An associative array where 'lat' => approximate latitude of the center of the postal code's area 'lon' => approximate longitude of the center of the postal code's area
1 call to location_latlon_rough_default()
- location_latlon_rough in ./
location.inc - Takes an location and returns a "rough" latitude/longitude pair based on the postal code data available for the given country.
File
- ./
location.inc, line 294
Code
function location_latlon_rough_default($location = array()) {
if (!isset($location['country']) || !isset($location['postal_code'])) {
return NULL;
}
if ($row = db_query("SELECT latitude, longitude FROM {zipcodes} WHERE country = :country AND zip = :zip", array(
':country' => $location['country'],
':zip' => $location['postal_code'],
))
->fetchObject()) {
return array(
'lat' => $row->latitude,
'lon' => $row->longitude,
);
}
elseif (($newlatlong = location_latlon_exact($location)) != NULL) {
// try a one-time external geocoding
if ($newlatlong['lat']) {
// and store results in zipcodes table for next lookups being internally handled
// (yeah this is missing city/state info a.t.m., but is way better than nothing!)
$result = db_query("INSERT INTO {zipcodes} (latitude, longitude, country, zip) values (:latitude, :longitude, :country, :zip)", array(
':latitude' => $newlatlong['lat'],
':longitude' => $newlatlong['lon'],
':country' => $location['country'],
':zip' => $location['postal_code'],
));
}
return $newlatlong;
}
else {
return NULL;
}
}