K2xL
K2xL

Reputation: 10320

Google Maps Geo giving JSONP error Unexpected token : or invalid label

We're trying to get information of a user based on the lat longitude.

Our code has been working fine until a couple weeks ago when we started getting this error.

Here is our code:

var reverse_geo_url = "http://maps.google.com/maps/geo?q=" + lat + "," + lng + "&key=[key]&sensor=false"
console.log("Calling " + reverse_geo_url);
new $.ajax(reverse_geo_url, {
    dataType: "jsonp",
    timeout: 5000,
    success: function (data) {
         // yay
    }
});

Problem is that we all of a sudden are getting

Uncaught SyntaxError: Unexpected token :

Here is an example response from Google's servers (when I go directly to the url)

{
  "name": "33.0024912,-88.4218782",
  "Status": {
    "code": 200,
    "request": "geocode"
  },
  "Placemark": [ {
    "id": "p1",
    "address": "Paulette Rd, Macon, MS 39341, USA",
    "AddressDetails": {
   "Accuracy" : 6,
   "Country" : {
      "AdministrativeArea" : {
         "AdministrativeAreaName" : "MS",
         "Locality" : {
            "LocalityName" : "Macon",
            "PostalCode" : {
               "PostalCodeNumber" : "39341"
            },
            "Thoroughfare" : {
               "ThoroughfareName" : "Paulette Rd"
            }
         }
      },
      "CountryName" : "USA",
      "CountryNameCode" : "US"
   }
},
    "ExtendedData": {
      "LatLonBox": {
        "north": 32.9994289,
        "south": 32.9891822,
        "east": -88.4094885,
        "west": -88.4393238
      }
    },
    "Point": {
      "coordinates": [ -88.4238788, 32.9952338, 0 ]
    }
  } ]
}

https://developers.google.com/maps/documentation/javascript/geocoding#GeocodingResponses

Accessing the Geocoding service is asynchronous, since the Google Maps API needs to make a call to an external server. For that reason, you need to pass a callback method to execute upon completion of the request. This callback method processes the result(s). Note that the geocoder may return more than one result.

According to the documentation callback is supposed to be supported. JQuery adds that callback header so I don't get why Google isn't honoring it.

*Edit: For reference, the url that my app tries to call is http://maps.google.com/maps/geo?q=33.8024912,-84.4218782&key=[my key here]&sensor=false&callback=jQuery16409036452900618315_1335293866369&_=1335293866469 Is this something on Google's side? Or is something on our side? Again, this code used to work perfectly.

Other posts that I've seen on StackOverFlow, people are saying that when this error shows up in means that the server doesn't support JSONP... What's confusing to me is that this used to work... Did google change their API?

Upvotes: 1

Views: 2577

Answers (2)

LenPopLilly
LenPopLilly

Reputation: 1227

Reading through the docs it seems you don't need to register any more to use this api,

I used this successfully :

var url = string.Format("http://maps.google.com/maps/geo?q={0}&output=xml", encodedPostCode);

Hope this helps

Upvotes: 0

Mano Marks
Mano Marks

Reputation: 8819

Out of curiosity, why are you doing ajax calls to do geocoding? You could be using the Google Maps API directly and more simply: https://developers.google.com/maps/documentation/javascript/geocoding

Upvotes: 1

Related Questions