我对JSON相当陌生,并且试图使用curl从Google Maps API获取经过编码的城市的经纬度。我使用的功能是:使用PHP(curl)从JSON(Google Maps API)中提取数据
function geocode($city){
$cityclean = str_replace (" ", "+", $city);
$details_url = "http://maps.googleapis.com/maps/api/geocode/json?address=" . $cityclean . "&sensor=false";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $details_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$geoloc = json_decode(curl_exec($ch), true);
$step1 = $geoloc['results'];
$step2 = $step1['geometry'];
$coords = $step2['location'];
print $coords['lat'];
print $coords['lng'];
}
所有这一切的目标是,从结果拉纬度和经度值 - >几何 - >位置以下JSON的数组:
{
"status": "OK",
"results": [ {
"types": [ "locality", "political" ],
"formatted_address": "Westminster, London, UK",
"address_components": [ {
"long_name": "London",
"short_name": "London",
"types": [ "locality", "political" ]
}, {
"long_name": "Westminster",
"short_name": "Westminster",
"types": [ "administrative_area_level_3", "political" ]
}, {
"long_name": "Greater London",
"short_name": "Greater London",
"types": [ "administrative_area_level_2", "political" ]
}, {
"long_name": "England",
"short_name": "England",
"types": [ "administrative_area_level_1", "political" ]
}, {
"long_name": "United Kingdom",
"short_name": "GB",
"types": [ "country", "political" ]
} ],
"geometry": {
"location": {
"lat": 51.5001524,
"lng": -0.1262362
},
"location_type": "APPROXIMATE",
"viewport": {
"southwest": {
"lat": 51.3493528,
"lng": -0.3783580
},
"northeast": {
"lat": 51.7040647,
"lng": 0.1502295
}
},
"bounds": {
"southwest": {
"lat": 51.3493528,
"lng": -0.3783580
},
"northeast": {
"lat": 51.7040647,
"lng": 0.1502295
}
}
}
} ]
}
但是,它不打印任何东西。我知道该函数可以成功从Google获取JSON并将其带到我的服务器,就像我为'status'值做了一个打印语句,并返回'OK'。这个问题似乎是当我尝试深入研究JSON的时候。
对不起,如果这是一个简单的问题,但正如我所说,我是新来的,现在它让我疯狂。
非常感谢! :)
伟大的作品,非常感谢:)我认为这将是什么做的阵列,我只以前使用JSON获得从容器中的数据。再次感谢! :D – 2011-04-02 20:24:25
不客气:-)玩得开心! – 2011-04-02 20:26:39
嗨,有没有办法使用用户的ip获得纬度长? – 2014-05-26 10:50:59