Posted By

FreaKzero on 08/18/10


Tagged

php google geocode


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

convergine
arucordoba


Google Geocoding, send Query - get defined Information


 / Published in: PHP
 

Simple function to get Infos from google geocode with a query as an Array The standard API Key is the Key for LOCALHOST.

  1. function geoCode($QUERY,$GOOGLEKEY="ABQIAAAAmYQAbPSpbkj2fDNP_JB0UBT2yXp_ZAY8_ufC3CFXhHIE1NvwkxQQxl2km_LSoDtKnqMEqRRfxbaqjQ") {
  2. $RETURN = array();
  3. $page = file_get_contents("http://maps.google.com/maps/geo?q=".urlencode($QUERY)."&key=".$GOOGLEKEY."&output=xml");
  4. $xml = new SimpleXMLElement($page);
  5.  
  6. list($longitude, $latitude, $altitude) = explode(",",
  7. $xml->Response->Placemark->Point->coordinates);
  8.  
  9. $RETURN["LandCode"] = (string) $xml->Response->Placemark->AddressDetails->Country->CountryNameCode;
  10. $RETURN["LandName"] = (string) $xml->Response->Placemark->AddressDetails->Country->CountryName;
  11. $RETURN["AdArea"] = (string) $xml->Response->Placemark->AddressDetails->Country->AdministrativeArea->AdministrativeAreaName;
  12. $RETURN["SubAdArea"] = (string) $xml->Response->Placemark[0]->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->SubAdministrativeAreaName;
  13. $RETURN["DependentArea"] = (string) $xml->Response->Placemark[0]->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->Locality->DependentLocality->DependentLocalityName;
  14. $RETURN["Locality"] = (string) $xml->Response->Placemark->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->Locality->LocalityName;
  15.  
  16. $RETURN["PostCode"] = (int) $xml->Response->Placemark->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->Locality->PostalCode->PostalCodeNumber;
  17.  
  18. if (!$RETURN["PostCode"])
  19. $PLZ = (int) $xml->Response->Placemark[0]->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->Locality->DependentLocality->PostalCode->PostalCodeNumber;
  20.  
  21.  
  22. $RETURN["LAT"] = $latitude;
  23. $RETURN["LON"] = $longitude;
  24.  
  25. return $RETURN;
  26. }

Report this snippet  

You need to login to post a comment.