Geocoder doesn't always return a value

前端 未结 5 2024
感动是毒
感动是毒 2020-12-09 23:34

I am able to successfully get lat/long and pass it to the geocoder to get an Address. However, I don\'t always get an address back. Seems like it takes a couple of attempts?

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-10 00:26

    in case anyone is still looking for a fix, here is what i did: first, i made sure that all the points were fine (would load) but may not always get grabbed by geocoding (getting the data is hit or miss) second, we force the code to do what we want it to do, in other words, if we know it CAN find a lat and long, lets just make it keep trying until it does! this diddnt seem to effect my load time either! so here is the code:

    do {
    $geocode=file_get_contents('https://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&key='.$key.'sensor=false');
    
    $output= json_decode($geocode);
    $lat = $output->results[0]->geometry->location->lat;
    $long = $output->results[0]->geometry->location->lng;
    } while($lat == '' and $long == '');
    

    this will continue to loop until both lat and long are not empty strings!

    happy coding! :P

提交回复
热议问题