You are here

function gmap_geocode in GMap Module 7.2

Same name and namespace in other branches
  1. 5 gmap.module \gmap_geocode()
  2. 6.2 gmap.module \gmap_geocode()
  3. 6 gmap.module \gmap_geocode()
  4. 7 gmap.module \gmap_geocode()

Utility function to use the google maps geocoder server side.

@todo move this to GmapRequest class

This is an easy, quick way to geocode a single address. Note: This is a REMOTE CALL TO GOOGLE. Do NOT use this where performance matters, as it could possibly take several seconds for this function to return.

See also

http://www.google.com/apis/maps/documentation/reference.html#GGeoStatusCode

http://drupal.org/node/1940474

1 string reference to 'gmap_geocode'
gmap_location_update_5101 in ./gmap_location.install
Gmap 5.x-1.0 update 2.

File

./gmap.module, line 1214
GMap -- Routines to use the Google Maps API in Drupal.

Code

function gmap_geocode($address, $tld = 'com') {
  $data = drupal_http_request(gmap_views_protocol() . '://maps.googleapis.' . $tld . '/maps/api/geocode/json?address=' . urlencode($address) . '&sensor=false');
  if ($data->code == 200) {
    $data_decoded = json_decode($data->data);
    if ($data_decoded->status == 'ZERO_RESULTS' || $data_decoded->status == 'OVER_QUERY_LIMIT') {
      return array(
        'status' => $data_decoded->status,
        'accuracy' => NULL,
        'latitude' => NULL,
        'longitude' => NULL,
      );
    }
    else {
      return array(
        'status' => $data_decoded->status,
        'accuracy' => $data_decoded->results[0]->geometry->location_type,
        'latitude' => $data_decoded->results[0]->geometry->location->lat,
        'longitude' => $data_decoded->results[0]->geometry->location->lng,
      );
    }
  }

  // Non 200 is G_GEO_SERVER_ERROR (500).
  return array(
    'status' => 500,
  );
}