$url = "http://maps.google.com/maps/geo?q=".urlencode($q)."&key=$apikey";
// sendRequest
// note how referer is set manually
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$body = curl_exec($ch);
curl_close($ch);
$this->json = json_decode ( $body );
list ( $zipcode, $localityName ) = array ( "test", "test" );
$i=0;
// Loop through Google-Maps Placemarks ( request from user input ) and compare with needle
foreach ($this->json->Placemark as $k) {
if ( $k->AddressDetails->Country->AdministrativeArea->PostalCode->PostalCodeNumber != NULL ) {
$place [ "zipcode" ] = $k->AddressDetails->Country->AdministrativeArea->PostalCode->PostalCodeNumber;
} elseif ( $k->AddressDetails->Country->AdministrativeArea->Locality->PostalCode->PostalCodeNumber != NULL ) {
$place [ "zipcode" ] = $k->AddressDetails->Country->AdministrativeArea->Locality->PostalCode->PostalCodeNumber;
} elseif ( $k->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->PostalCode->PostalCodeNumber != NULL ) {
$place [ "zipcode" ] = $k->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->PostalCode->PostalCodeNumber;
} elseif ( $k->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->Locality->PostalCode->PostalCodeNumber != NULL ) {
$place [ "zipcode" ] = $k->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->Locality->PostalCode->PostalCodeNumber;
} elseif ( $k->AddressDetails->Country->AdministrativeArea->AdministrativeAreaName != NULL ) {
// If not found repeat the query but this time with the Zipcode and the AdministrativeAreaName ( if found )
$plz->query = $zipcode." ".$k->AddressDetails->Country->AdministrativeArea->AdministrativeAreaName;
$google = $this->geocode($plz->query, $plz->apikey);
foreach ($google->Placemark as $i) {
if ($i->AddressDetails->Country->AdministrativeArea->PostalCode->PostalCodeNumber != NULL) {
$place [ "zipcode" ] = $i->AddressDetails->Country->AdministrativeArea->PostalCode->PostalCodeNumber;
} elseif ($i->AddressDetails->Country->AdministrativeArea->Locality->PostalCode->PostalCodeNumber != NULL) {
$place [ "zipcode" ] = $i->AddressDetails->Country->AdministrativeArea->Locality->PostalCode->PostalCodeNumber;
} elseif ($i->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->PostalCode->PostalCodeNumber != NULL) {
$place [ "zipcode" ] = $i->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->PostalCode->PostalCodeNumber;
} elseif ($i->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->Locality->PostalCode->PostalCodeNumber != NULL) {
$place [ "zipcode" ] = $i->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->Locality->PostalCode->PostalCodeNumber;
}
}
}
// Just in case that the user has typos, get the locality name of a random placemark
if ($k->AddressDetails->Country->AdministrativeArea->Locality->LocalityName != NULL) {
$place [ "name" ] = $code['ortsname'] = utf8_encode($k->AddressDetails->Country->AdministrativeArea->Locality->LocalityName);
} elseif ($k->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->Locality->LocalityName != NULL) {
$place [ "name" ] = $code['ortsname'] = utf8_encode($k->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->Locality->LocalityName);
} elseif ($k->AddressDetails->Country->AdministrativeArea->AdministrativeAreaName != NULL) {
$place [ "name" ] = $code['ortsname'] = utf8_encode($k->AddressDetails->Country->AdministrativeArea->AdministrativeAreaName);
}
// If zipcode or city is true
if ( $place [ "zipcode" ] == $zipcode || $place [ "name" ] == $localityName ) {
... do stuff ...
}