Search code examples
phpjsonmapquest

Getting data from JSON (Using Mapquest and PHP)


So, I'm trying to pair Google and MapQuest's geocoding abilities because some address aren't able to be geocoded through Google, but they show up on Mapquest so I want to pair them. I was able to get the google results:

$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;

How do I get the results using MapQuest? I've never used MapQuest so I have no idea how it returns the data and I haven't found anything on here or anywhere that demonstrates retrieving the data...

HELP! Thanks!


Solution

  • You can use Jay Sheth's sample code then get latitude and longitude:

    $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;