I have a website where user posts their picture and then adds a post code so you could see where this picture is taken. I have function where I can find latitude and longitude by inserting a post code:
function googlemaps($postcode){
$search_code = urlencode($postcode);
$url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . $search_code . '&sensor=true';
$json = json_decode(file_get_contents($url));
$lat = $json->results[0]->geometry->location->lat;
$lng = $json->results[0]->geometry->location->lng;
$url2 = '
<a href="http://maps.google.com/maps?q=' . $lat . ',' . $lng . '" target="blank">
<img src="http://maps.google.com/maps/api/staticmap?size=400x240&zoom=15&maptype=roadmap&sensor=false&markers=icon:|' . $lat . ',' . $lng . '">
</a>
<div class="show"></div>
';
return $url2;
}
Now for the testing I made 10 records in db with different post codes, but the problem is when I output all of the records with the locations, every single time some of the locations are blank, and after refreshing page they change for some reason (I mean if location A was blank first time, after reload location A will be ok but location B goes blank) and by blank I mean they doesnt output any latitude and longitude information and my image looks like this:
and the link for this image is:
<img src="https://maps.google.com/maps/api/staticmap?size=400x240&zoom=15&maptype=roadmap&sensor=false&markers=icon:,">
I believe there is something to do with the function and when 10 records are fetched from db this function doesnt manage to get the information quickly? Because sometimes after refreshing pages I can get all of the locations showing, sometimes just 1 location is showing.