Sunday, May 16, 2010

PHP Geocoding with the Google Maps API

PHP Geocoding with the Google Maps API

<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

ini_set('max_execution_time', 0);

/**
 * [] First, sign up for a key. http://code.google.com/apis/maps/signup.html
 *
 * [] A few people have mentioned that after some number of requests (between 100-300)
 * in quick succession google will kill your access for the day, so you may want to insert a
 *
 * sleep(1);
 *
 * To stagger the requests a little.
 *
 * [] Other Resources: http://code.google.com/p/php-google-map-api/
 *
 * [] Note: GEOCODE (Geospatial Entity Object Code) is a standardized all-natural number 
 * representation format specification for geospatial coordinate measurements that 
 * provide details of the exact location of geospatial point at, below, or above the surface 
 * of the earth at a specified moment of time.
 * Reference: http://en.wikipedia.org/wiki/Geocode
 */

//Set up our variables
$longitude = "";
$latitude = "";
$precision = "";

//Three parts to the querystring: q is address, output is the format, key is the GAPI key
$key = 'Your_Key_Here';

//$address = urlencode("1600 Amphitheatre Parkway");  // specify postal code or address.
$address = urlencode("94043");  // specify postal code or address.

### output_format:
### xml: 
### csv: status, accuracy, latitude, longitude
### json:
$output_format = 'json'; // xml, csv, or json.

$url = 'http://maps.google.com/maps/geo?q=' . $address . '&output=' . $output_format . '&key=' . $key;

echo $url . '

'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER,0); curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $data = curl_exec($ch); curl_close($ch); //echo '

';
//print_r($data);
//echo '
'; if ($output_format == 'json') { $data_json = json_decode($data); ### If status 200 okay. if ($data_json->Status->code == '200') { echo 'longitude: ' . $data_json->Placemark[0]->Point->coordinates[0]; // longitude echo ' '; echo 'latitude: ' . $data_json->Placemark[0]->Point->coordinates[1]; // latitude } echo '
';
  print_r($data_json);
  echo '
'; } elseif ($output_format == 'csv') { $data_csvArr = str_getcsv_mem($data); ### If status 200 okay. if ($data_csvArr[0] == '200') { echo 'accuracy: ' . $data_csvArr[1]; // accuracy echo 'latitude: ' . $data_csvArr[2]; // latitude echo ' '; echo 'longitude: ' . $data_csvArr[3]; // longitude } } function str_getcsv_mem($input, $delimiter = ",", $enclosure = '"', $escape = "\\") { $outputArr = Null; $oneHundredKBs = 1 * 100 * 1024; $fp = fopen('php://temp/maxmemory:' . $oneHundredKBs, 'r+'); fputs($fp, $input); rewind($fp); $rowCount = 0; while (($row = fgetcsv($fp, 0, $delimiter, $enclosure)) !== FALSE) { //$escape only got added in 5.3.0 foreach ($row as $key => $col) { $outputArr[$rowCount][$key] = $col; } $rowCount++; } fclose($fp); return $outputArr; } /* stdClass Object ( [name] => 94043 [Status] => stdClass Object ( [code] => 200 [request] => geocode ) [Placemark] => Array ( [0] => stdClass Object ( [id] => p1 [address] => Mountain View, CA 94043, USA [AddressDetails] => stdClass Object ( [Accuracy] => 5 [Country] => stdClass Object ( [AdministrativeArea] => stdClass Object ( [AdministrativeAreaName] => CA [SubAdministrativeArea] => stdClass Object ( [Locality] => stdClass Object ( [LocalityName] => Mountain View [PostalCode] => stdClass Object ( [PostalCodeNumber] => 94043 ) ) [SubAdministrativeAreaName] => Santa Clara ) ) [CountryName] => USA [CountryNameCode] => US ) ) [ExtendedData] => stdClass Object ( [LatLonBox] => stdClass Object ( [north] => 37.4505349 [south] => 37.38567 [east] => -122.03277 [west] => -122.10842 ) ) [Point] => stdClass Object ( [coordinates] => Array ( [0] => -122.0723816 [1] => 37.428434 [2] => 0 ) ) ) ) ) */ ?>

No comments: