1

I know there are plenty of the same question with answers all over but I just can't seem to solve mine.

The code that I used:

$.getJSON("http://maps.googleapis.com/maps/api/geocode/json?latlng=" + results[0].geometry.location.lat() + "," + results[0].geometry.location.lng() + "&sensor=false&callback=?", function (data) {
    if (data[0]) {
        alert(data[0].formatted_address);
    }
});     

I can get the json result when I go to the url but not within my application. If I remove the &callback=? I get XmlHttpRequest error but if I do I get the titled error. I've read from some answers that I'm actually supposed to be expecting a json with a format like ({json-content}) instead of {json-content} which I am getting. If so, how do I get the correct format?

Edit

JSON output:

{
   "results" : [
     Uncaught SyntaxError: Unexpected token :
  {
     "address_components" : [
        {
           "long_name" : "81",
           "short_name" : "81",
           "types" : [ "street_number" ]
        },
        {
           "long_name" : "Grange Rd",
           "short_name" : "Grange Rd",
           "types" : [ "route" ]
        },
        {
           "long_name" : "Tanglin",
           "short_name" : "Tanglin",
           "types" : [ "sublocality", "political" ]
        },
        {
           "long_name" : "Singapore",
           "short_name" : "Singapore",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Singapore",
           "short_name" : "SG",
           "types" : [ "country", "political" ]
        },
        {
           "long_name" : "249586",
           "short_name" : "249586",
           "types" : [ "postal_code" ]
        }
     ],
     "formatted_address" : "81 Grange Rd, Singapore 249586",
     "geometry" : {
        "location" : {
           "lat" : 1.3029390,
           "lng" : 103.8230960
        },
        "location_type" : "ROOFTOP",
        "viewport" : {
           "northeast" : {
              "lat" : 1.304287980291502,
              "lng" : 103.8244449802915
           },
           "southwest" : {
              "lat" : 1.301590019708498,
              "lng" : 103.8217470197085
           }
        }
     },
     "types" : [ "street_address" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "249586",
           "short_name" : "249586",
           "types" : [ "postal_code" ]
        },
        {
           "long_name" : "Tanglin",
           "short_name" : "Tanglin",
           "types" : [ "sublocality", "political" ]
        },
        {
           "long_name" : "Singapore",
           "short_name" : "Singapore",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Singapore",
           "short_name" : "SG",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Singapore 249586",
     "geometry" : {
        "location" : {
           "lat" : 1.3029390,
           "lng" : 103.8230960
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 1.304287980291502,
              "lng" : 103.8244449802915
           },
           "southwest" : {
              "lat" : 1.301590019708498,
              "lng" : 103.8217470197085
           }
        }
     },
     "types" : [ "postal_code" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "248649",
           "short_name" : "248649",
           "types" : [ "postal_code" ]
        },
        {
           "long_name" : "Orchard",
           "short_name" : "Orchard",
           "types" : [ "sublocality", "political" ]
        },
        {
           "long_name" : "Singapore",
           "short_name" : "Singapore",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Singapore",
           "short_name" : "SG",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Singapore 248649",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 1.30441750,
              "lng" : 103.82452430
           },
           "southwest" : {
              "lat" : 1.30206850,
              "lng" : 103.82307520
           }
        },
        "location" : {
           "lat" : 1.3030750,
           "lng" : 103.8242460
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 1.304591980291502,
              "lng" : 103.8251487302915
           },
           "southwest" : {
              "lat" : 1.301894019708498,
              "lng" : 103.8224507697085
           }
        }
     },
     "types" : [ "postal_code" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "248643",
           "short_name" : "248643",
           "types" : [ "postal_code" ]
        },
        {
           "long_name" : "River Valley",
           "short_name" : "River Valley",
           "types" : [ "sublocality", "political" ]
        },
        {
           "long_name" : "Singapore",
           "short_name" : "Singapore",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Singapore",
           "short_name" : "SG",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Singapore 248643",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 1.3032470,
              "lng" : 103.82452430
           },
           "southwest" : {
              "lat" : 1.30206850,
              "lng" : 103.82307520
           }
        },
        "location" : {
           "lat" : 1.3021720,
           "lng" : 103.8244280
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 1.304006730291502,
              "lng" : 103.8251487302915
           },
           "southwest" : {
              "lat" : 1.301308769708498,
              "lng" : 103.8224507697085
           }
        }
     },
     "types" : [ "postal_code" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "Tanglin",
           "short_name" : "Tanglin",
           "types" : [ "sublocality", "political" ]
        },
        {
           "long_name" : "Singapore",
           "short_name" : "Singapore",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Singapore",
           "short_name" : "SG",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Tanglin, Singapore",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 1.32350930,
              "lng" : 103.83548650
           },
           "southwest" : {
              "lat" : 1.2919460,
              "lng" : 103.80224340
           }
        },
        "location" : {
           "lat" : 1.29907550,
           "lng" : 103.8164260
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 1.32350930,
              "lng" : 103.83548650
           },
           "southwest" : {
              "lat" : 1.2919460,
              "lng" : 103.80224340
           }
        }
     },
     "types" : [ "sublocality", "political" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "Singapore",
           "short_name" : "SG",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Singapore",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 1.47077350,
              "lng" : 104.0843810
           },
           "southwest" : {
              "lat" : 1.19737070,
              "lng" : 103.61036090
           }
        },
        "location" : {
           "lat" : 1.3520830,
           "lng" : 103.8198360
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 1.47077350,
              "lng" : 104.0843810
           },
           "southwest" : {
              "lat" : 1.19737070,
              "lng" : 103.61036090
           }
        }
     },
     "types" : [ "country", "political" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "Singapore",
           "short_name" : "Singapore",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Singapore",
           "short_name" : "SG",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Singapore",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 1.47077350,
              "lng" : 104.0843810
           },
           "southwest" : {
              "lat" : 1.19737070,
              "lng" : 103.61036090
           }
        },
        "location" : {
           "lat" : 1.28943650,
           "lng" : 103.84998020
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 1.47077350,
              "lng" : 104.0843810
           },
           "southwest" : {
              "lat" : 1.19737070,
              "lng" : 103.61036090
           }
        }
     },
     "types" : [ "locality", "political" ]
  }
   ],
   "status" : "OK"
}

JSON link address:

http://maps.googleapis.com/maps/api/geocode/json?latlng=1.302939,103.82309600000008&sensor=false&callback=jQuery17208982370237354189_1341999317106&_=1341999317754

5
  • 1
    Can you post the json output you are actually getting. Or a full example json request address. Commented Jul 11, 2012 at 9:55
  • +1 to showing us full example url Commented Jul 11, 2012 at 9:57
  • @JonTaylor I've just added the result and request address Commented Jul 11, 2012 at 10:05
  • possible duplicate of Retrieving JSON throws error: Unexpected token :, jQuery and Google Maps json response, and many others. Commented Jul 11, 2012 at 10:05
  • @RobW I've seen the links that you've mentioned before posting this question but had believed due to the different use of .ajax and .getJSON, I would not have to go back to javascript map api. I understand if it will be closed as a duplicate. Commented Jul 11, 2012 at 10:14

1 Answer 1

2

The support for JSONP on this api has been dropped - meaning its returning JSON not JSONP - which is why your getting that error

Duplicate answer here gives a good alternative or something like this :

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">

var geocoder = new google.maps.Geocoder();
var latlong = new google.maps.LatLng("37.5097258429375","-122.27783203125");

geocoder.geocode({
    'latLng': latlong
}, function(results, status) {

    if (status == google.maps.GeocoderStatus.OK) {
        alert(results[0].formatted_address);
    }
});​
</script>

Working example here

API docs are here

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

8 Comments

Does that mean I should be using .ajax instead?
@consprice No, just use the Maps API.
Could he not just get rid of the &callback part which is the bit that jQuery makes it assume is JSONP
@JonTaylor no .. it returns JSON and this wont work because of the same origin policy
If you're referring to the javascript api, I'm using this because I the javascript isn't working for me.
|

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.