doumubi6784 2013-08-31 17:13
浏览 174
已采纳

Google Geocode API - 在PHP中回显特定的详细信息

I'm using this script to get the geo-code information from Google but I don't know how to filter out just specifics such as Lat, Lon, Town, County. Here is my code (which I beleive will work under the new API-3?):

<?php
$address="LS1 7AS";
$result=file_get_contents("http://maps.google.com/maps/api/geocode/json?address=" . urlencode($address) . "&sensor=false" );
    $geocodedinfo=json_decode($result);

print_r($geocodedinfo);
?>

This prints out all of the information I need but as a block, I just want to have some of them so I can insert it into a table, for example:

$town = "town from result";
$county = "county from result";
$lat = "lat from result";
$lon = "lon from result";
  • 写回答

2条回答 默认 最新

  • donglu1472 2013-08-31 17:55
    关注

    First of all You need to modify your API output format.Use output format json because you have already used json_decode function.

    $address="LS1 7AS";
    "http://maps.google.com/maps/api/geocode/json?address=" . urlencode($address) . "&sensor=false";
    

    And you variables should look like :

    $geocodedinfo=json_decode($result);
    

    You have to check return status :

    if($geocodedinfo->status == 'OK') {
         $country = $geocodeinfo->results[0]->address_components[3]->long_name;
         $town = $geocodeinfo->results[0]->address_components[2]->long_name;
         $lat = $geocodeinfo->results[0]->geometry->location->lat;
         $lon = $geocodeinfo->results[0]->geometry->location->lng;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分