我正在使用Google Geocode API从我发送的自定义语言环境中获取坐标和国家/地区。
https://maps.googleapis.com/maps/api/geocode/json?address=13 vernon vernon park singapore 367835&amkey=########################################
我从中得到的回应是
{
"results" : [
{
"address_components" : [
{
"long_name" : "13",
"short_name" : "13",
"types" : [ "street_number" ]
},
{
"long_name" : "Vernon Park",
"short_name" : "Vernon Park",
"types" : [ "route" ]
},
{
"long_name" : "Toa Payoh",
"short_name" : "Toa Payoh",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Singapore",
"short_name" : "Singapore",
"types" : [ "locality", "political" ]
},
{
"long_name" : "367835",
"short_name" : "367835",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "13 Vernon Park, Singapore 367835",
"geometry" : {
"location" : {
"lat" : 1.340062,
"lng" : 103.880577
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 1.341410980291502,
"lng" : 103.8819259802915
},
"southwest" : {
"lat" : 1.338713019708498,
"lng" : 103.8792280197085
}
}
},
"place_id" : "ChIJbXRmsZAX2jERv_hlVeqBXtM",
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
我写的php代码是从响应中获得国家的
foreach($response['results'][0]['address_components'] as $addressComponet){
if(in_array('country', $addressComponet['types'])) {
if($addressComponet['short_name'] != $addressComponet['long_name'])
$country = $addressComponet['long_name'];
else
$country = $addressComponet['long_name'];
}
}
$geometry = $response['results'][0]['geometry'];
$longitude = $geometry['location']['lng'];
$latitude = $geometry['location']['lat'];
$array = array(
'latitude' => $latitude,
'longitude' => $longitude,
'location_type' => $geometry['location_type'],
'country' => $country
);
其中 $response
包含curl_exec响应。直到昨天,这似乎一直很好。但是,从今天开始,我没有在阵列中获得乡村弦。我不知道的API响应有什么变化吗?
我认为响应发生了变化,所以我继续在Google Maps API问题部分提交错误。
https://code.google.com/p/gmaps-api-issues/issues/detail?id=11024
原来,他们在地理编码API中添加了一个新的正向地理编码器。他们还添加了一个标志,将请求重定向到该API中的旧前向地理编码器。
现在的重点是,由于它们升级了地理编码API,因此我们应该如何处理数据库中存在的此类语言环境?我们已经在使用Google Maps AutoComplete来防止随机无用的虚假环境通过API进行。
通常是地理编码API返回与有关国家的地址格式相关的地址组件。
新加坡的典型地址为:
Block 123 Marina Avenue 2 #15-222 Singapore 123456
6 Ayer Rajah Crescent Singapore 139962
它们不包括国家名称,因此地理编码API不返回此地址组件,因为它与地址格式无关。
更新:
如果您需要检查特定地区的国家/
例如。
搜索'6 Ayer Rajah Crescent Singapore 139962'
https://maps.googleapis.com/maps/api/geocode/json?address=6 Ayer RAJAH CRESCENT CRESCENT SINGAPORE 139962&kekey= your_api_key
您将获得坐标1.294947,103.787603。现在,使用等于国家的结果类型执行反向地理编码。
https://maps.googleapis.com/maps/api/geocode/json?latlng=1.294947,103.787603& result_type = country&armult_type = country&amkey = your_api_api_key
响应将为
{
"results":[
{
"address_components":[
{
"long_name":"Singapore",
"short_name":"SG",
"types":[
"country","political"
]
}
],
"formatted_address":"Singapore",
"geometry":{
"bounds":{
"northeast":{
"lat":1.4784001,"lng":104.0945001
},
"southwest":{
"lat":1.1496,"lng":103.594
}
},
"location":{
"lat":1.352083,"lng":103.819836
},
"location_type":"APPROXIMATE",
"viewport":{
"northeast":{
"lat":1.4707592,"lng":104.0884808
},
"southwest":{
"lat":1.1587023,"lng":103.6055575
}
}
},
"place_id":"ChIJdZOLiiMR2jERxPWrUs9peIg",
"types":[
"country","political"
]
}
],
"status":"OK"
}