Skip to content Skip to sidebar Skip to footer

Google Places Api - Android

I am making an android application that needs to search in my local area within 10km and display the results onto a map using pins, For example: 'Starbucks', 'Wallmart', Shopping m

Solution 1:

As far as the HTTP interface goes, your code LGTM assuming API_KEY holds a valid API key. That is, one created in the API console. Try printing out the whole request.url and see if it looks like this:

https://maps.googleapis.com/maps/api/place/search/json?key=AIzaSyDoTeTuPXXXXXXXXXMHPVYM5VTg&location=37.994682,-87.6045923&radius=500&sensor=false

Also see this thread because e.response isn't valid, maybe just remove that call to println and see what e looks like when it's thrown.

Solution 2:

You can do this by using Google API JAVA Client - Here is an example using the java client for getting all the 60 results.

public PlacesList search(double latitude, double longitude, double radius, String types)throws Exception {

        try {

            HttpRequestFactoryhttpRequestFactory= createRequestFactory(HTTP_TRANSPORT);
            HttpRequestrequest= httpRequestFactory
                    .buildGetRequest(newGenericUrl("https://maps.googleapis.com/maps/api/place/search/json?"));
            request.getUrl().put("key", YOUR_API_KEY);
            request.getUrl().put("location", latitude + "," + longitude);
            request.getUrl().put("radius", radius); 
            request.getUrl().put("sensor", "false");
            request.getUrl().put("types", types);

            PlacesListlist= request.execute().parseAs(PlacesList.class);

            if(list.next_page_token!=null || list.next_page_token!=""){
                         Thread.sleep(4000);
                         /*Since the token can be used after a short time it has been  generated*/
                request.getUrl().put("pagetoken",list.next_page_token);
                PlacesListtemp= request.execute().parseAs(PlacesList.class);
                list.results.addAll(temp.results);

                if(temp.next_page_token!=null||temp.next_page_token!=""){
                    Thread.sleep(4000);
                    request.getUrl().put("pagetoken",temp.next_page_token);
                    PlacesListtempList=  request.execute().parseAs(PlacesList.class);
                    list.results.addAll(tempList.results);
              }

            }
            return list;

        } catch (HttpResponseException e) {
            returnnull;
        }

    }

Post a Comment for "Google Places Api - Android"