Question

I have the following code (I didn't program this site originally), but not sure what it does as every time it runs it use to throw a google error 610 and why we need to update to V3, but now it skips to the else statement of 'Geocoding skipped'. It was programs for Google Goecoding v2 and I am trying to update it to v3. Like I said someone else did it and left and I am the one trying to fix it now that it is broken.

$geocode = !($listing  
    && $listing->getAddress1() == $commonFieldValues['address_1'] 
    && $listing->getAddress2() == $commonFieldValues['address_2'] 
    && $listing->getCity() == $commonFieldValues['city'] 
    && $listing->getState() == $commonFieldValues['state']
    && $listing->getZipCode() == $commonFieldValues['zip_code']
);

if($geocode) {
    $baseUrl = 'http://maps.google.com/maps/geo?output=xml&key=' . OurMLS_Constants::getGoogleMapsApiKey();
    $url = $baseUrl . '&q=' . urlencode(join(', ', $listing->getAddressLines(true, true)));
    $xml = simplexml_load_file($url);
    if($xml !== false) {
        // Curious what the status codes mean see...
        // http://code.google.com/apis/maps/documentation/reference.html#GGeoStatusCode
        $status = $xml->Response->Status->code;
        if($status != '200') {
            print "Google Maps geocode request returned with status code '$status'\n";
        } else {
            $coordinates = $xml->Response->Placemark->Point->coordinates;
            $coordinatesSplit = split(",", $coordinates);
            $latitude = (float) $coordinatesSplit[1];
            $longitude = (float) $coordinatesSplit[0];
            $listing->setLatitude($latitude);
            $listing->setLongitude($longitude);             
        }
    }
} else {
    print ', Geocoding skipped';
}

I have updated it to what I though would get me the same results in V3 (as the rest of the site and mapping works as long as we can pull Lat and Long into the database) (not paid to touch what isn't broken)

$geocode = !($listing  
    && $listing->getAddress1() == $commonFieldValues['address_1'] 
    && $listing->getAddress2() == $commonFieldValues['address_2'] 
    && $listing->getCity() == $commonFieldValues['city'] 
    && $listing->getState() == $commonFieldValues['state']
    && $listing->getZipCode() == $commonFieldValues['zip_code']
);

if($geocode) {
    $baseUrl = 'http://maps.googleapis.com/maps/api/geocode/xml';
    $url = $baseUrl . "?address=" . urlencode(join(', ', $listing->getAddressLines(true, true))) . "&sensor=false" ;
    $xml = simplexml_load_file($url);
    if($xml !== false) {
        $status = $xml->GeocodeResponse->status;
        if($status != 'OK') {
            print "Google Maps geocode request returned with status code '$status'\n";
        } else {
            $latitude = $xml->GeocodeResponse->geometry->location->lat;
            $longitude = $xml->GeocodeResponse->geometry->location->lng;
            $listing->setLatitude($latitude);
            $listing->setLongitude($longitude);
        }
    }
} else {
    print ', Geocoding skipped';
}

Any help would be appreciated, this is a cron job to pull the RETS feed and save to a DB and as it saves the information to a local DB it should geocode the address in the DB so when the listings are shown they have a google map.

I am really not sure what "$geocode = !(" does. This loop does run for each address it pulls and everything works except the geocoding.

I am not sure why it was pulling and getting Status code but now it is just skipping it

here are the two xml files http://maps.google.com/maps/geo?output=xml&q=1600%pennsylvania%20ave%20washington%20dc http://maps.googleapis.com/maps/api/geocode/xml?address=1600%pennsylvania%20ave%20washington%20dc&sensor=false

Was it helpful?

Solution

$geocode = !(...bunch of stuff...); says "take that bunch of stuff and evaluate it as a boolean value (ie. true or false). Then negate it (ie. if it's true, make it false; if it's false, make it true)." Check out the documentation for more on logical operators.

So you need to start by looking at the stuff in the parentheses:

$listing  // does the listing object exist?
&& $listing->getAddress1() == $commonFieldValues['address_1']   // and does "address1" equal this other thing
&& $listing->getAddress2() == $commonFieldValues['address_2']   // and does "address2" equal this other thing 
&& $listing->getCity() == $commonFieldValues['city']   // and does "city" equal this other thing
&& $listing->getState() == $commonFieldValues['state']   // you get the picture...
&& $listing->getZipCode() == $commonFieldValues['zip_code']

If all that is true, then $geocode is going to be false (because of the ! operator) and your geocoding is only going to take place if $geocode is true.

I don't know much about your code, but I'd interpret this as saying

If I have a listing and the address doesn't match or the city doesn't match or the state doesn't match or the zip code doesn't match, then I need to geocode it.

So look at your $listing object (does it exist?) and your $commonFieldValues array (what are those values and are they what you expect them to be?).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top