【问题标题】:Google Places API - androidGoogle Places API - 安卓
【发布时间】:2012-02-19 15:47:24
【问题描述】:

我正在制作一个 android 应用程序,需要在本地 10 公里内搜索并使用图钉将结果显示在地图上,例如:“星巴克”、“沃尔玛”、购物中心等。我的搜索词在我的活动课程中指定。并且要明确:我不想在谷歌地图中打开搜索,我希望它在我自己的应用程序中显示结果。但是我在执行搜索的代码中遇到错误。错误出现在以下几点:

Url: url 无法解析或不是字段 Execute:未为 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;
      }
}

【问题讨论】:

    标签: android api search maps


    【解决方案1】:

    您可以使用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;
            }
    
        }
    

    【讨论】:

      【解决方案2】:

      就 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 被抛出时的样子。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-02
        • 2014-03-18
        • 2012-02-22
        • 1970-01-01
        相关资源
        最近更新 更多