donkey199024 2015-01-27 16:17
浏览 37

谷歌地理编码和邮政编码查找失败

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.

  • 写回答

1条回答 默认 最新

  • dongpo9071 2015-01-27 16:39
    关注

    Well I find the solution.

    Because I used this function when my results were fetched from db it might hit the limitation of the lookups or something.

    Now I get the latitude and longitude when user posts their image and enters their post code, I extract with the function latitude and longitude from the post code and then insert the results in db.

    Now even if I fetch 10 results from the database, because I have all locations sorted out already, all maps shows with no problems.

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度