我正在处理php中的Web服务,该服务可以从客户端调用,该服务器通过查询询问服务的某些纬度和经度,从数据库获取地址并使用google api:
$geocode=file_get_contents("https://maps.google.com/maps/api/geocode/json?address=".$row."&components=country:IT&sensor=true");
之后,服务会废弃内容并获取经度和纬度
$lat = $address->results[0]->geometry->location->lat;
$lng = $address->results[0]->geometry->location->lng;
问题是lat和lng是错误的但如果我从浏览器尝试地址我得到正确的lat和lng。
例如,这是我从" http://maps.google.com/maps/api/geocode/json?address=Via%20Corno%20di%20Cavento,%20Milan,%20Metropolita&components=country:IT&sensor=false%22"
获得的{
"results" : [
{
"address_components" : [
{
"long_name" : "Via Corno di Cavento",
"short_name" : "Via Corno di Cavento",
"types" : [ "route" ]
},
{
"long_name" : "Milano",
"short_name" : "Milano",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Milan",
"short_name" : "Milan",
"types" : [ "administrative_area_level_3", "political" ]
},
{
"long_name" : "Città Metropolitana di Milano",
"short_name" : "MI",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Lombardia",
"short_name" : "Lombardia",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Italy",
"short_name" : "IT",
"types" : [ "country", "political" ]
},
{
"long_name" : "20148",
"short_name" : "20148",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Via Corno di Cavento, 20148 Milano, Italy",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 45.4731517,
"lng" : 9.1267511
},
"southwest" : {
"lat" : 45.47025660000001,
"lng" : 9.1236354
}
},
"location" : {
"lat" : 45.471653,
"lng" : 9.125879299999999
},
"location_type" : "GEOMETRIC_CENTER",
"viewport" : {
"northeast" : {
"lat" : 45.4731517,
"lng" : 9.1267511
},
"southwest" : {
"lat" : 45.47025660000001,
"lng" : 9.1236354
}
}
},
"partial_match" : true,
"place_id" : "ChIJZwCky4_BhkcRuGIAMLrYfsY",
"types" : [ "route" ]
}
],
"status" : "OK"
}
如果我从php服务运行该查询,这就是我得到的:
"{
"results" : [
{
"address_components" : [
{
"long_name" : "Italy",
"short_name" : "IT",
"types" : [ "country", "political" ]
} ],
"formatted_address" : "Italy",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 47.092,
"lng" : 18.5205015
},
"southwest" : {
"lat" : 35.4929201,
"lng" : 6.6267201
}
},
"location" : {
"lat" : 41.87194,
"lng" : 12.56738
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 47.092,
"lng" : 18.5205015
},
"southwest" : {
"lat" : 35.4929201,
"lng" : 6.6267201
}
}
},
"partial_match" : true,
"place_id" : "ChIJA9KNRIL-1BIRb15jJFz1LOI",
"types" : [ "country", "political" ]
} ],
"status" : "OK"}"
有人可以帮助我吗?