wordSmith
wordSmith

Reputation: 3183

parsing JSON returned from Google Maps API

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);
});

Upvotes: 0

Views: 715

Answers (2)

Paul Roub
Paul Roub

Reputation: 36438

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)

Upvotes: 1

void
void

Reputation: 36703

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]);
                }
            }
        }

Upvotes: 0

Related Questions