简体   繁体   中英

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 mall, etc. The search word that i specify in my activity class. And to be clear: I do NOT want to open the search in Google maps, i want it to display the results inside MY own application. But i get an error at the code that executes the search. The error comes up on the following things:

Url: url cannot be resolved or is not a field Execute: The method execute() is undefined for the type HttpRequest Response: response cannot be resolved or is not a field

I am using three packages: com.mycompany.applicationname = Default package, containing main code, including the search code com.mycompany.applicationname.Model = Containing PlaceAutoComplete, PlacesList, Place, etc. com.mycompany.applicationname.PlacesRequests = Containing PlaceRequest.java

Please help me, i really need help and thanks SO much in advance

This is the code that i am using to execute the search:

        private static final String PLACES_SEARCH_URL =  "https://maps.googleapis.com/maps/api/place/search/json?";

    private static final boolean PRINT_AS_STRING = false;


     public void performSearch() throws Exception {
      try {
       System.out.println("Perform Search ....");
       System.out.println("-------------------");
       HttpRequestFactory httpRequestFactory = createRequestFactory(transport);
       HttpRequest request = httpRequestFactory.buildGetRequest(new GenericUrl(PLACES_SEARCH_URL));
       request.url.put("key", API_KEY);
       request.url.put("location", lat + "," + lng);
       request.url.put("radius", 500);
       request.url.put("sensor", "false");

       if (PRINT_AS_STRING) {
        System.out.println(request.execute().parseAsString());
       } else {

        PlacesList places = request.execute().parseAs(PlacesList.class);
        System.out.println("STATUS = " + places.status);
        for (Place place : places.results) {
         System.out.println(place);
        }
       }

      } catch (HttpResponseException e) {
       System.err.println(e.response.parseAsString());
       throw e;
      }
}

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 {

            HttpRequestFactory httpRequestFactory = createRequestFactory(HTTP_TRANSPORT);
            HttpRequest request = httpRequestFactory
                    .buildGetRequest(new GenericUrl("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);

            PlacesList list = 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);
                PlacesList temp = 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);
                    PlacesList tempList =  request.execute().parseAs(PlacesList.class);
                    list.results.addAll(tempList.results);
              }

            }
            return list;

        } catch (HttpResponseException e) {
            return null;
        }

    }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM