Google Places API - 安卓 [英] Google Places API - android

查看:27
本文介绍了Google Places API - 安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

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

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

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

推荐答案

您可以使用 Google API JAVA 客户端 - 这是一个使用 Java 客户端获取所有 60 个结果的示例.

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

    }

这篇关于Google Places API - 安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆