0

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 Answer 1

2

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)

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much. I would never had guessed that, or if I did, it would be a week too late!
You're welcome ;) I honestly just know because I spent the last 2 weeks working with JSONP (until we decided to use sth else instead..)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.