0

I'm trying to geocode an address, in this case, Google's HQ.

When I try to make the jQuery $.ajax() request to GET the JSON, it says Uncaught SyntaxError: Unexpected token :

I can't seem to figure out where the unexpected : is. What I'm trying to do is decode the lat and long from an address as a string and use the Places API to find mechanics near those coordinates.

$.ajax({
    url: 'https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=myKey',
    dataType: 'jsonp',
    jsonp: 'callback',
    method: 'GET',
    success: function(results){
        console.log(results);
        queryLat = results['results']['geometry']['location']['lat'];
        queryLong = results['results']['geometry']['location']['lng'];
        console.log('latitude: '+queryLat);
        console.log('longitude: '+queryLong);


        function callback(mapsResults, status){
            if(status == google.maps.places.PlacesServiceStatus.OK){
                for(var i = 0; i < mapResults.length; i++){
                    var place = mapResultsi];
                    createMarker(mapResults[i]);
                }
            }
        }
        var map;
        var service;

        map = new google.maps.Map(document.getElementById('map'), {
            center: new google.maps.LatLng(queryLat, queryLong),
            zoom: 15
        });

        var request ={
            location: new google.maps.LatLng(queryLat, queryLong)
            radius:'500',
            query:'mechanic'
        };

        service = new google.maps.places.PlacesService(map);
        service.textSearch(request, callback);
});
6
  • 1
    var place = mapResultsi]; should be var place = mapResults[i]; Commented Oct 1, 2014 at 21:08
  • 1
    And you're missing a comma after location: new google.maps.LatLng(queryLat, queryLong) Commented Oct 1, 2014 at 21:13
  • @PaulRoub Actually, I'm still getting the error. It seems to be just in the geocoding part where I find the lat and long as when I remove that and manually enter a lat and long, it works. Commented Oct 1, 2014 at 21:21
  • 1
    You're using jsonp when you want json as your dataType. The Google API is returning a JSON string, not a function to be evaluated. Commented Oct 1, 2014 at 21:27
  • @PaulRoub Ah, got it. Can you post that as an answer. The reason I use jsonp is because sometimes it bypasses cross-domain. Or at least is has for me in the past. Commented Oct 1, 2014 at 21:31

2 Answers 2

1

Three problems:

  1. Telling $.ajax() to expect jsonp when you're calling a JSON-returning API
  2. var place = mapResultsi]; should be var place = mapResults[i];
  3. missing a comma after location: new google.maps.LatLng(queryLat, queryLong)
Sign up to request clarification or add additional context in comments.

Comments

0
function callback(mapsResults, status){
            if(status == google.maps.places.PlacesServiceStatus.OK){
                for(var i = 0; i < mapResults.length; i++){
                    **var place = mapResultsi];**
                    createMarker(mapResults[i]);
                }
            }
        }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.