gpt4 book ai didi

php - 从地址获取州和县

转载 作者:可可西里 更新时间:2023-11-01 08:40:04 26 4
gpt4 key购买 nike

我想通过 Google API 使用地理编码从地址中获取州和县。这是我的代码:

$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$street.''.$plz.''.$city.'&sensor=false');
$output= json_decode($geocode);
$lat = $output->results[0]->geometry->location->lat;
$long = $output->results[0]->geometry->location->lng;
$formatted_adress = $output->results[0]->formatted_address;
$state = $output->results[0]->address_components->administrative_area_level_2;
$county = $output->results[0]->address_components->administrative_area_level_1;

latitudelongitudeformatted_adress 可以找到并且我可以将结果保存在我的数据库中,但是州和县不起作用.

这是输出:

    {
"results" : [
{
"address_components" : [
{
"long_name" : "Hauptgasse",
"short_name" : "Hauptgasse",
"types" : [ "route" ]
},
{
"long_name" : "Solothurn",
"short_name" : "Solothurn",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Solothurn",
"short_name" : "Solothurn",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Solothurn",
"short_name" : "Südost",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Schweiz",
"short_name" : "CH",
"types" : [ "country", "political" ]
},
{
"long_name" : "4500",
"short_name" : "4500",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Hauptgasse, 4500 Solothurn, Schweiz",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 47.2088079,
"lng" : 7.540025399999999
},
"southwest" : {
"lat" : 47.2065432,
"lng" : 7.535295899999999
}
},
"location" : {
"lat" : 47.20778,
"lng" : 7.537360000000001
},
"location_type" : "GEOMETRIC_CENTER",
"viewport" : {
"northeast" : {
"lat" : 47.20902453029149,
"lng" : 7.540025399999999
},
"southwest" : {
"lat" : 47.20632656970849,
"lng" : 7.535295899999999
}
}
},
"partial_match" : true,
"place_id" : "ChIJS4BfGfnXkUcRqLUKnJzN54U",
"types" : [ "route" ]
}
],
"status" : "OK"
}

最佳答案

根据您显示的 json 数据,您需要获取 statecounty,如下所示:-

$state = $output->results[0]->address_components[2]->long_name;// i assume `Solothurn` is state name
$county = $output->results[0]->address_components[4]->long_name; // i assume `Schweiz` is county name

如果县名也是 Solothurn 那么只需像下面这样更改县代码:-

$county = $output->results[0]->address_components[3]->long_name;

关于php - 从地址获取州和县,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35248226/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com