Google地图使用json从file_get_contents获取位置地址

时间:2015-04-16 19:13:15

标签: php json google-maps

通过以下操作获得经度和经度。我也可以获得地址吗?如果是,请有人帮我修改下面的代码以获取lat和long的地址。

$zipcode= 10003;
$json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$zipcode&sensor=false");
$json = json_decode($json);
$lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};

1 个答案:

答案 0 :(得分:0)

您可以从响应中获取格式化地址:

$address = $json->{'results'}[0]->{'formatted_address'};

您还可以单独获取每个组件:

$zipcode    = $json->{'results'}[0]->{'address_components'}[0]->{'long_name'};
$city       = $json->{'results'}[0]->{'address_components'}[1]->{'long_name'};
$department = $json->{'results'}[0]->{'address_components'}[2]->{'long_name'};
$region     = $json->{'results'}[0]->{'address_components'}[3]->{'long_name'};
$country    = $json->{'results'}[0]->{'address_components'}[4]->{'long_name'};