Home
TYPO3 Extensions
TYPO3 Webseiten
Innovation Award
Kundenlogin
Code-Suche
Internet-Suche
Google Trends
Code-Schnipsel
Links
Kontakt

Fail-Safe-Algorithmn to retrieve a Geo-Point from Google-Maps:

  1. $url = "http://maps.google.com/maps/geo?q=".urlencode($q)."&key=$apikey";
  2.  
  3.  
  4.  
  5. // sendRequest
  6.  
  7. // note how referer is set manually
  8.  
  9.  
  10.  
  11. $ch = curl_init();
  12.  
  13. curl_setopt($ch, CURLOPT_URL, $url);
  14.  
  15. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  16.  
  17. $body = curl_exec($ch);
  18.  
  19. curl_close($ch);
  20.  
  21. $this->json = json_decode ( $body );
  22.  
  23.  
  24.  
  25. list ( $zipcode, $localityName ) = array ( "test", "test" );
  26.  
  27. $i=0;
  28.  
  29.  
  30.  
  31. // Loop through Google-Maps Placemarks ( request from user input ) and compare with needle
  32.  
  33. foreach ($this->json->Placemark as $k) {
  34.  
  35. if ( $k->AddressDetails->Country->AdministrativeArea->PostalCode->PostalCodeNumber != NULL ) {
  36.  
  37. $place [ "zipcode" ] = $k->AddressDetails->Country->AdministrativeArea->PostalCode->PostalCodeNumber;
  38.  
  39. } elseif ( $k->AddressDetails->Country->AdministrativeArea->Locality->PostalCode->PostalCodeNumber != NULL ) {
  40.  
  41. $place [ "zipcode" ] = $k->AddressDetails->Country->AdministrativeArea->Locality->PostalCode->PostalCodeNumber;
  42.  
  43. } elseif ( $k->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->PostalCode->PostalCodeNumber != NULL ) {
  44.  
  45. $place [ "zipcode" ] = $k->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->PostalCode->PostalCodeNumber;
  46.  
  47. } elseif ( $k->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->Locality->PostalCode->PostalCodeNumber != NULL ) {
  48.  
  49. $place [ "zipcode" ] = $k->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->Locality->PostalCode->PostalCodeNumber;
  50.  
  51. } elseif ( $k->AddressDetails->Country->AdministrativeArea->AdministrativeAreaName != NULL ) {
  52.  
  53.  
  54.  
  55. // If not found repeat the query but this time with the Zipcode and the AdministrativeAreaName ( if found )
  56.  
  57. $plz->query = $zipcode." ".$k->AddressDetails->Country->AdministrativeArea->AdministrativeAreaName;
  58.  
  59. $google = $this->geocode($plz->query, $plz->apikey);
  60.  
  61.  
  62.  
  63. foreach ($google->Placemark as $i) {
  64.  
  65. if ($i->AddressDetails->Country->AdministrativeArea->PostalCode->PostalCodeNumber != NULL) {
  66.  
  67. $place [ "zipcode" ] = $i->AddressDetails->Country->AdministrativeArea->PostalCode->PostalCodeNumber;
  68.  
  69. } elseif ($i->AddressDetails->Country->AdministrativeArea->Locality->PostalCode->PostalCodeNumber != NULL) {
  70.  
  71. $place [ "zipcode" ] = $i->AddressDetails->Country->AdministrativeArea->Locality->PostalCode->PostalCodeNumber;
  72.  
  73. } elseif ($i->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->PostalCode->PostalCodeNumber != NULL) {
  74.  
  75. $place [ "zipcode" ] = $i->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->PostalCode->PostalCodeNumber;
  76.  
  77. } elseif ($i->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->Locality->PostalCode->PostalCodeNumber != NULL) {
  78.  
  79. $place [ "zipcode" ] = $i->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->Locality->PostalCode->PostalCodeNumber;
  80.  
  81. }
  82.  
  83. }
  84.  
  85. }
  86.  
  87.  
  88.  
  89. // Just in case that the user has typos, get the locality name of a random placemark
  90.  
  91. if ($k->AddressDetails->Country->AdministrativeArea->Locality->LocalityName != NULL) {
  92.  
  93. $place [ "name" ] = $code['ortsname'] = utf8_encode($k->AddressDetails->Country->AdministrativeArea->Locality->LocalityName);
  94.  
  95. } elseif ($k->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->Locality->LocalityName != NULL) {
  96.  
  97. $place [ "name" ] = $code['ortsname'] = utf8_encode($k->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->Locality->LocalityName);
  98.  
  99. } elseif ($k->AddressDetails->Country->AdministrativeArea->AdministrativeAreaName != NULL) {
  100.  
  101. $place [ "name" ] = $code['ortsname'] = utf8_encode($k->AddressDetails->Country->AdministrativeArea->AdministrativeAreaName);
  102.  
  103. }
  104.  
  105.  
  106.  
  107. // If zipcode or city is true
  108.  
  109. if ( $place [ "zipcode" ] == $zipcode || $place [ "name" ] == $localityName ) {
  110.  
  111. ... do stuff ...
  112.  
  113. }
© 2005-2011 Chi Hoang. All rights reserved. All trademarks are property of their respective owners. Last Update: 
16.05.2012
bookmark in your browserbookmark at mister wongpublish in twitterbookmark at del.icio.usbookmark at digg.combookmark at furl.netbookmark at linksilo.debookmark at reddit.combookmark at spurl.netbookmark at technorati.combookmark at google.combookmark at yahoo.combookmark at facebook.combookmark at stumbleupon.combookmark at propeller.combookmark at newsvine.combookmark at jumptags.com