consprice
consprice

Reputation: 722

getJSON Uncaught SyntaxError: Unexpected token :

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

Upvotes: 1

Views: 4426

Answers (1)

Manse
Manse

Reputation: 38147

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

Upvotes: 2

Related Questions