反向地理编码不能给出正确的位置


Reverse Geocoding does not give the right locations

我有一个编码的XML文件,其中包含多个ride。我想使用反向地理编码来检索实际位置。但是当我上传XML文件时,刷新页面时会得到不同的位置。它似乎是缓存,因为有很多位置,与上面的位置相同。

//Get Location Start
    //Get location
$url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=".$locBegLat.",".$locBegLon."&sensor=false";
$data = @file_get_contents($url);
$jsondata = json_decode($data,true);
if(is_array($jsondata) && $jsondata['status'] == "OK"){
//street
        foreach ($jsondata["results"] as $result) {
foreach ($result["address_components"] as $address) {
    if (in_array("route", $address["types"])) {
        $streetBeg = $address["long_name"];
    }
}
}
//street_number
               foreach ($jsondata["results"] as $result) {
    foreach ($result["address_components"] as $address) {
        if (in_array("street_number", $address["types"])) {
            $street_numberBeg = $address["long_name"];
        }
    }
   }
// city
foreach ($jsondata["results"] as $result) {
    foreach ($result["address_components"] as $address) {
        if (in_array("locality", $address["types"])) {
            $cityBeg = $address["long_name"];
    }
    }
    }
// postal_code
foreach ($jsondata["results"] as $result) {
    foreach ($result["address_components"] as $address) {
        if (in_array("postal_code", $address["types"])) {
            $postal_codeBeg = $address["long_name"];
    }
}
}
// country
foreach ($jsondata["results"] as $result) {
foreach ($result["address_components"] as $address) {
    if (in_array("country", $address["types"])) {
        $countryBeg = $address["long_name"];
    }
}
}
}
    $LocBeg = $streetBeg . " " . $street_numberBeg; 
echo $streetBeg;    
echo $street_numberBeg;
echo $cityBeg;  
echo $countryBeg;
echo $postal_codeBeg;

网址:www.interwebmedia.nl/dataxi

xml文件:https://drive.google.com/file/d/0B31rNYjTJf81dkQ4R2xOcUc2WEk/edit?usp=sharing

我希望有人知道,为什么使用相同坐标的位置不同,只需在上传文件后刷新页面即可。

有不同的问题,两个最基本的问题:

  1. 对于地理编码,存在每秒10个请求的限制。在每个请求之后打印$jsondata['status'],您会看到很多请求都因为限制而失败。解决方案(不能100%保证):在每次请求之前致电usleep(100000);,以确保第二个中的请求不超过10个

  2. 您在循环中创建的变量,例如$streetEnd$cityEnd,必须在每个循环开始时重新设置。否则,当该组件在当前响应中不可用时,脚本将使用上一个请求中设置的值。