Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use Google Places to get an array of Place names?

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>
like image 719
Davenport Avatar asked Nov 05 '22 21:11

Davenport


1 Answers

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);
like image 186
no.good.at.coding Avatar answered Nov 12 '22 12:11

no.good.at.coding