PHP房地产谷歌Geocoder更新V2到V3

时间:2014-04-25 17:26:16

标签: php xml google-maps google-maps-api-3 google-geocoder

我有以下代码(我最初没有对此网站进行编程),但不确定每次运行时它的作用是用于抛出谷歌错误610以及为什么我们需要更新到V3,但现在它跳到'Geocoding skipped'的else语句。这是Google Goecoding v2的程序,我正在尝试将其更新为v3。就像我说的那样,其他人做了并离开了,我就是那个试图解决它的人,因为它已经坏了。

$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';
}

我已经更新了它,但我会在V3中获得相同的结果(因为我们可以将Lat和Long拉入数据库,因为其他网站和映射工作)(没有付费触摸什么不是'破了)

$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';
}

任何帮助将不胜感激,这是一个cron作业拉动RETS提要并保存到数据库,因为它将信息保存到本地数据库,它应该对数据库中的地址进行地理编码,所以当显示列表时,他们有谷歌地图。

我真的不确定是什么“$ geocode =!(”的确如此。这个循环确实会为它提取的每个地址运行,除了地理编码之外一切正常。

我不确定为什么它会拉动并获取状态代码,但现在它只是跳过它

这是两个xml文件 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

1 个答案:

答案 0 :(得分:0)

$geocode = !(...bunch of stuff...);说"拿出那堆东西并将其评估为布尔值(即真或假)。然后否定它(即,如果它是真的,那就是假的;如果它是假的,那就把它变成真的)。"有关逻辑运算符的更多信息,请查看the documentation

所以你需要从括号中的东西开始:

$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']

如果一切正确,那么$geocode将会是假的(因为!运算符),并且只有在$geocode为真时才会进行地理编码。< / p>

我对您的代码了解不多,但我将此解释为

  

如果我有商家信息且地址没有匹配或城市匹配或状态没有&#39; t 匹配或邮政编码没有匹配,那么我需要对其进行地理编码。

那么看看你的$listing对象(它存在吗?)和你的$commonFieldValues数组(那些值是什么,它们是你期望的那些?)。

相关问题