5

I'm trying to access Google Places to return an array of 50 restaurant names around a latitude and longitude, but I've hit a brick wall when it comes to making the request. I've got the code to determine the user's latitude and longitude and I've gotten my API key from Google, I just cannot figure out how to make the request via the URL and the Google documentation is silent on how to actually get the data. I feel confident that I'll be able to figure out how to parse the JSON return, but getting the data has proven more difficult/confusing than I imagined.

If it matters, I'm using MaxMind to get the user's latitude and longitude. Here is the script that I have so far:

<script type="text/javascript" src="https://www.google.com/jsapi?key=api_key"></script>
<script type="text/javascript" src="http://j.maxmind.com/app/geoip.js"></script>

<script type="text/javascript">
    latitude = geoip_latitude();
    longitude = geoip_longitude();
</script>
Davenport
  • 219
  • 3
  • 9

1 Answers1

2

Google documentation is silent on how to actually get the data

Not really, the Places Web Service API documentation has examples - you generate the URL with the appropriate parameters and you get back JSON or XML which you parse.

If you were using jQuery and the API supported JSONP, you could have done something like this:

$.ajax({
        url: 'https://maps.googleapis.com/maps/api/place/search/json', 
        data: {
                location:"-33.8670522,151.1957362", 
                radius: 500, 
                types:'food', 
                name: 'harbour', 
                sensor: false, 
                key: 'AIzaSyAiFpFd85eMtfbvmVNEYuNds5TEF9FjIPI'
            }, 
        success: function(data){
                    //data will be a JSON object that you can iterate over
                },
        dataType: 'jsonp'
    });

Note that this will NOT work since JSONP is not supported, this is only to illustrate how you might make requests in jQuery

However, AFAIK, the web service does not support JSONP so you won't be able to make requests via JavaScript. You could, however, make use of the Google Maps JavaScript API and use the Places JavaScript library instead. Again, the page has examples showing how to use the library, the basic use will involve initiating the service and making a search request as below (copied straight from the docs):

service = new google.maps.places.PlacesService(map);
service.search(request, callback);
no.good.at.coding
  • 20,221
  • 2
  • 60
  • 51
  • 1
    Maybe I'm retarded when it comes to Google's API, but I don't see any real examples of how to make the request in JavaScript. The API has many examples of how the JSON looks after the call and how to parse it, but I was hoping for a cut-and-dry "here's how to make the request". I'm used to the Java API, which is very clear, in my opinion, so Google's API looks more like a semantical explanation rather than a syntactical explanation to me. – Davenport May 28 '11 at 02:52
  • @Davenport Are you familiar with ajax? That's how you would make a request in JavaScript. But, as I've noted, you can't use the web service API because it doesn't support JSONP which means no cross-domain JavaScript requests are allowed. – no.good.at.coding May 28 '11 at 03:00
  • I'm not familiar with Ajax, unfortunately. I've been reading up on it, but my knowledge is scant, at best. And as far as JSON vs. JSONP, how would this affect my ability to parse the results into an array? – Davenport May 28 '11 at 03:09
  • @Davenport Ok. Either way, it doesn't really matter much here since you can't use this API through JavaScript in the browser unless you add some server side component to act as a proxy or to fetch the data for you. You should see the JavaScript library linked to in the answer and post back if you have a problem with that. – no.good.at.coding May 28 '11 at 03:15