0
votes

I'm using Mapquest in an attempt to get latitude and longitude values of a requested address.

This is my call to their API:

$json = file_get_contents('http://www.mapquestapi.com/geocoding/v1/address?key=******&callback=renderOptions&outFormat=json&inFormat=json&json={location:{street:"Lancaster,PA"},options:{thumbMaps:false}}');

$output = json_decode($json, true);

$lat = $output['results'][0]['locations'][0]['latLng']['lat'];
$lng = $output['results'][0]['locations'][0]['latLng']['lng'];

echo $lat.",".$lng;

As you can see, I'm trying to echo the LAT and LNG to screen but it's empty. When I print_r($json), I get the following:

renderOptions({"results":[{"locations":[{"latLng":{"lng":-76.30127,"lat":40.03804},"adminArea4":"Lancaster County","adminArea5Type":"City"...............

So I know the response is OK and that LAT and LNG are available, but if I print_r($output) I get nothing and if I var_dump($output) I get NULL, and obviously I don't get my LAT LNG data.

I'm not great with JSON and arrays. Can somebody see what I'm doing wrong?

1

1 Answers

2
votes

You need to remove &callback=renderOptions from the URL. Right now, the server returns JSONP (with the specified callback function). This is no valid JSON and so json_decode returns NULL (as specified in the manual)