Getting data from JSON (Using Mapquest and PHP)

4.3k views Asked by At

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!

2

There are 2 answers

1
domi On BEST ANSWER

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;
1
Jay Sheth On

this code should start you off:

<?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
?>

More info: http://open.mapquestapi.com/geocoding/