Question

I have an encoded XML-file, which contains multiple rides. I want to use reverse geocoding for retrieving the actual locations. But when I upload the XML file, I get different locations when I refresh the page. It seems to be the cache, because there are a lot locations, which are the same as the ones above.

//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;

the website is: www.interwebmedia.nl/dataxi

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

I hope someone knows, why the locations are different using the same coordinates, just by refreshing the page after uploading the file.

Was it helpful?

Solution

There are different issues, the 2 most basic:

  1. there is a limit of 10 requests/sec for geocoding. Print $jsondata['status'] after each request and you'll see that a lot of the requests fail because of the limit. Solution(not 100% guaranteed): Call usleep(100000); before each request to be sure that there are not more than 10 requests in a second

  2. the variables that you create in the loop, e.g. $streetEnd , $cityEnd must be resetted at the begin of each loop . Otherwise, when this component is not available in the current response, your script will use the value set in a previous request.

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