从JSON获取数据(使用Mapquest和PHP)


Getting data from JSON (Using Mapquest and PHP)

所以,我正在尝试将Google和MapQuest的地理编码功能配对,因为某些地址无法通过Google进行地理编码,但它们显示在Mapquest上,所以我想将它们配对。我能够得到谷歌结果:

$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false');
$output= json_decode($geocode);
$lat1 = $output->results[0]->geometry->location->lat;
$lon1 = $output->results[0]->geometry->location->lng;

如何使用地图任务获得结果?我从未使用过 MapQuest,所以我不知道它是如何返回数据的,我在这里或任何地方都没有找到任何演示检索数据的内容......

帮助!谢谢!

您可以使用Jay Sheth的示例代码,然后获取纬度和经度:

$json = file_get_contents('http://open.mapquestapi.com/geocoding/v1/address?key={your_key_here}&location=Lancaster,PA');
$jsonArr = json_decode($json);
$lat1 = $jsonArr->results[0]->locations[0]->latLng->lat;
$lon1 = $jsonArr->results[0]->locations[0]->latLng->lng;

这段代码应该让你开始:

<?php
//Important: do not pass the callback=xyz parameter (as stated in the docs)
$json = file_get_contents('http://open.mapquestapi.com/geocoding/v1/address?key={your_key_here}&location=Lancaster,PA');
$jsonArr = json_decode($json);
print_r($jsonArr);
//Access latitude, longitude, etc. from PHP standard object
?>

更多信息:http://open.mapquestapi.com/geocoding/