You are here

function locationmap_geocode_for_address in Location Map 7

Same name and namespace in other branches
  1. 8.2 locationmap.module \locationmap_geocode_for_address()
  2. 7.2 locationmap.module \locationmap_geocode_for_address()

Returns latitude and longitude for $address or NULL if it cannot be found. @returns FALSE if address not found

3 calls to locationmap_geocode_for_address()
locationmapTest::test_locationmap_geocode_for_address in tests/locationmap.test
locationmapTest::test_locationmap_geocode_for_address_not_found in tests/locationmap.test
locationmap_geocode_for_address_recursive in ./locationmap.module
Try to get lat and lng information from address removing parts of address if not found.

File

./locationmap.module, line 252

Code

function locationmap_geocode_for_address($address) {
  $gmap_key = variable_get('locationmap_key', NULL);
  if (!$gmap_key) {
    return FALSE;
  }
  $address = urlencode($address);
  $url = "http://maps.google.com/maps/geo?q={$address}&key={$gmap_key}&output=csv";
  $options = array(
    'http' => array(
      'max_redirects' => 10,
      'timeout' => 120,
    ),
  );
  $context = stream_context_create($options);
  $page = @file_get_contents($url, false, $context);
  if (!$page) {
    return FALSE;
  }
  $matches = explode(',', $page);
  if (!($matches[0] == '200')) {
    return FALSE;
  }
  return array(
    $matches[2],
    $matches[3],
  );
}