Google Places API - android

时间:2012-01-26 14:56:37

标签: android api search maps

我正在制作一个需要在10公里范围内在我的本地区域搜索的Android应用程序,并使用图钉将结果显示在地图上,例如:“Starbucks”,“Wallmart”,购物中心等。搜索词我是在我的活动类中指定。并且要明确:我不想在Google地图中打开搜索,我希望它在我自己的应用程序中显示结果。但是我在执行搜索的代码中遇到错误。该错误出现在以下方面:

网址:网址无法解析或不是字段 执行:对于HttpRequest类型,方法execute()未定义 响应:响应无法解析或不是字段

我使用三个包: com.mycompany.applicationname =默认包,包含主要代码,包括搜索代码 com.mycompany.applicationname.Model =包含PlaceAutoComplete,PlacesList,Place等。 com.mycompany.applicationname.PlacesRequests =包含PlaceRequest.java

请帮助我,我真的需要帮助,并提前多多感谢

这是我用来执行搜索的代码:

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

2 个答案:

答案 0 :(得分:0)

就HTTP接口而言,假设API_KEY的代码LGTM拥有有效的API密钥。也就是说,在API控制台中创建一个。尝试打印出整个request.url,看看它是否如下所示:

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

另请参阅this thread,因为e.response无效,可能只是移除对println的调用,看看e在投掷时的样子。

答案 1 :(得分:0)

您可以使用Google API JAVA Client执行此操作 - 以下是使用Java客户端获取所有60个结​​果的示例。

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

    }