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 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果