我想通过Google地图API从经度和纬度获取国家/地区,密码,州和城市名称。但有时它的工作正确,但有时它会得到错误的信息,很多时候会返回空值,因为结果是动态的而不是固定的。
所以任何人都建议我如何使这个动态和内部检查是国家城市pincode和状态可用于任何数组索引从结果....
下面是我的代码......
$fromlat = $this->request->data['Order']['from_lat'];
$fromlong = $this->request->data['Order']['from_long'];
$geocode = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng='.$fromlat.','.$fromlong.'&sensor=false');
$json = json_decode($geocode);
//print_r($geocode);
$country = $json->{'results'}[1]->{'address_components'}[3]->{'long_name'};
$state = $json->{'results'}[1]->{'address_components'}[2]->{'long_name'};
$city = $json->{'results'}[1]->{'address_components'}[1]->{'long_name'};
$locality = $json->{'results'}[1]->{'address_components'}[0]->{'long_name'};
$pincode = $json->{'results'}[1]->{'address_components'}[4]->{'long_name'};
echo $locality."</br>".$country."</br>".$state."</br>".$city."</br>".$pincode;
我需要在结果forloop中检查地址组件中的类型,如果可用,则显示结果。
types = country = country name;
types = administrative_area_level_1 = state name ;
types = administrative_area_level_2 = city name ;
types = postal_code = pincode ;
下面是我的结果数组......
stdClass Object
(
[results] => Array
(
[0] => stdClass Object
(
[address_components] => Array
(
[0] => stdClass Object
(
[long_name] => Kurkheda - Wadsa - Bramhapuri - Nagpur Highway
[short_name] => MH MSH 9
[types] => Array
(
[0] => route
)
)
[1] => stdClass Object
(
[long_name] => Nagpur
[short_name] => Nagpur
[types] => Array
(
[0] => administrative_area_level_2
[1] => political
)
)
[2] => stdClass Object
(
[long_name] => Maharashtra
[short_name] => MH
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[3] => stdClass Object
(
[long_name] => India
[short_name] => IN
[types] => Array
(
[0] => country
[1] => political
)
)
[4] => stdClass Object
(
[long_name] => 441204
[short_name] => 441204
[types] => Array
(
[0] => postal_code
)
)
)
[formatted_address] => Kurkheda - Wadsa - Bramhapuri - Nagpur Hwy, Maharashtra 441204, India
[geometry] => stdClass Object
(
[bounds] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 21.0633475
[lng] => 79.1652473
)
[southwest] => stdClass Object
(
[lat] => 21.0599518
[lng] => 79.1646723
)
)
[location] => stdClass Object
(
[lat] => 21.0616013
[lng] => 79.1647236
)
[location_type] => GEOMETRIC_CENTER
[viewport] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 21.0633475
[lng] => 79.166308780291
)
[southwest] => stdClass Object
(
[lat] => 21.0599518
[lng] => 79.163610819708
)
)
)
[place_id] => ChIJj5wNX6O51DsRz53GLG9aZRM
[types] => Array
(
[0] => route
)
)
[1] => stdClass Object
(
[address_components] => Array
(
[0] => stdClass Object
(
[long_name] => Kalamana
[short_name] => Kalamana
[types] => Array
(
[0] => locality
[1] => political
)
)
[1] => stdClass Object
(
[long_name] => Nagpur
[short_name] => Nagpur
[types] => Array
(
[0] => administrative_area_level_2
[1] => political
)
)
[2] => stdClass Object
(
[long_name] => Maharashtra
[short_name] => MH
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[3] => stdClass Object
(
[long_name] => India
[short_name] => IN
[types] => Array
(
[0] => country
[1] => political
)
)
[4] => stdClass Object
(
[long_name] => 441204
[short_name] => 441204
[types] => Array
(
[0] => postal_code
)
)
)
[formatted_address] => Kalamana, Maharashtra 441204, India
[geometry] => stdClass Object
(
[bounds] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 21.0628156
[lng] => 79.164412
)
[southwest] => stdClass Object
(
[lat] => 21.0589309
[lng] => 79.1617083
)
)
[location] => stdClass Object
(
[lat] => 21.0605174
[lng] => 79.1629662
)
[location_type] => APPROXIMATE
[viewport] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 21.0628156
[lng] => 79.164412
)
[southwest] => stdClass Object
(
[lat] => 21.0589309
[lng] => 79.1617083
)
)
)
[place_id] => ChIJ2yhznKO51DsRGZWqNrtKBD4
[types] => Array
(
[0] => locality
[1] => political
)
)
[2] => stdClass Object
(
[address_components] => Array
(
[0] => stdClass Object
(
[long_name] => 441204
[short_name] => 441204
[types] => Array
(
[0] => postal_code
)
)
[1] => stdClass Object
(
[long_name] => Nagpur
[short_name] => Nagpur
[types] => Array
(
[0] => administrative_area_level_2
[1] => political
)
)
[2] => stdClass Object
(
[long_name] => Maharashtra
[short_name] => MH
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[3] => stdClass Object
(
[long_name] => India
[short_name] => IN
[types] => Array
(
[0] => country
[1] => political
)
)
)
[formatted_address] => Maharashtra 441204, India
[geometry] => stdClass Object
(
[bounds] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 21.139709
[lng] => 79.3452018
)
[southwest] => stdClass Object
(
[lat] => 20.8714046
[lng] => 79.0962536
)
)
[location] => stdClass Object
(
[lat] => 20.9903768
[lng] => 79.2451149
)
[location_type] => APPROXIMATE
[viewport] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 21.139709
[lng] => 79.3452018
)
[southwest] => stdClass Object
(
[lat] => 20.8714046
[lng] => 79.0962536
)
)
)
[place_id] => ChIJKTHfbpOw1DsRGwYyMATAfU4
[types] => Array
(
[0] => postal_code
)
)
[3] => stdClass Object
(
[address_components] => Array
(
[0] => stdClass Object
(
[long_name] => Nagpur
[short_name] => Nagpur
[types] => Array
(
[0] => administrative_area_level_2
[1] => political
)
)
[1] => stdClass Object
(
[long_name] => Maharashtra
[short_name] => MH
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[2] => stdClass Object
(
[long_name] => India
[short_name] => IN
[types] => Array
(
[0] => country
[1] => political
)
)
)
[formatted_address] => Nagpur, Maharashtra, India
[geometry] => stdClass Object
(
[bounds] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 21.713785
[lng] => 79.65179
)
[southwest] => stdClass Object
(
[lat] => 20.5841799
[lng] => 78.2515599
)
)
[location] => stdClass Object
(
[lat] => 21.3170198
[lng] => 79.1999949
)
[location_type] => APPROXIMATE
[viewport] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 21.713785
[lng] => 79.65179
)
[southwest] => stdClass Object
(
[lat] => 20.5841799
[lng] => 78.2515599
)
)
)
[place_id] => ChIJ691n-cTA1DsRh3HGEikxZ2E
[types] => Array
(
[0] => administrative_area_level_2
[1] => political
)
)
[4] => stdClass Object
(
[address_components] => Array
(
[0] => stdClass Object
(
[long_name] => Maharashtra
[short_name] => MH
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[1] => stdClass Object
(
[long_name] => India
[short_name] => IN
[types] => Array
(
[0] => country
[1] => political
)
)
)
[formatted_address] => Maharashtra, India
[geometry] => stdClass Object
(
[bounds] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 22.027909
[lng] => 80.890924
)
[southwest] => stdClass Object
(
[lat] => 15.602412
[lng] => 72.6593629
)
)
[location] => stdClass Object
(
[lat] => 19.7514798
[lng] => 75.7138884
)
[location_type] => APPROXIMATE
[viewport] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 22.027909
[lng] => 80.890924
)
[southwest] => stdClass Object
(
[lat] => 15.602412
[lng] => 72.6593629
)
)
)
[place_id] => ChIJ-dacnB7EzzsRtk_gS5IiLxs
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[5] => stdClass Object
(
[address_components] => Array
(
[0] => stdClass Object
(
[long_name] => India
[short_name] => IN
[types] => Array
(
[0] => country
[1] => political
)
)
)
[formatted_address] => India
[geometry] => stdClass Object
(
[bounds] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 35.5087008
[lng] => 97.395561
)
[southwest] => stdClass Object
(
[lat] => 6.7535159
[lng] => 68.1623859
)
)
[location] => stdClass Object
(
[lat] => 20.593684
[lng] => 78.96288
)
[location_type] => APPROXIMATE
[viewport] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 35.5087008
[lng] => 97.395561
)
[southwest] => stdClass Object
(
[lat] => 6.7535159
[lng] => 68.1633666
)
)
)
[place_id] => ChIJkbeSa_BfYzARphNChaFPjNc
[types] => Array
(
[0] => country
[1] => political
)
)
)
[status] => OK
)
答案 0 :(得分:2)
提取地址分量值的正确方法如下:
if (results[0]) {
$("input[name=lat]").val(results[0].geometry.location.lat());
$("input[name=lng]").val(results[0].geometry.location.lng());
var addressArr = [];
$.each(results[0].address_components, function (key, val) {
if (!$.inArray("country", results[0].address_components[key].types)) {
addressArr['country'] = results[0].address_components[key].long_name;
}
if (!$.inArray("administrative_area_level_1", results[0].address_components[key].types)) {
addressArr['state'] = results[0].address_components[key].long_name;
}
if (!$.inArray("administrative_area_level_2", results[0].address_components[key].types)) {
addressArr['city'] = results[0].address_components[key].long_name;
}
});
console.log(addressArr);
}
}
答案 1 :(得分:0)
我有一些类似的代码可以使用。基本上,如果可能有多个结果,它可以返回结果数组,如果只找到一个结果,则只返回一个对象。我的用例略有不同,但希望它有所帮助。试试这个:
1 | (100) - (50 + 0) = 50 |
1 | (100) - (30 + 50) = 20 |
2 | (200) - (20 + 0) = 180 |
2 | (200) - (150 + 20) = 30 |