谷歌API的地方 - 机器人 [英] Google Places API - android

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

问题描述

我想提出一个Android应用程序,需要在10km以内在我的本地搜索和使用引脚显示结果到地图上,例如:星巴克,Wallmart,商场等搜索词,我请在我的活动课。而且是明确的:我不想开在谷歌地图搜索,我希望它显示在我自己的应用程序的结果。但我得到一个错误,在执行该搜索code。该错误出现在以下几点:

URL:不能得到解决或不是场 执行:该方法execute()是未定义的类型型Htt prequest 响应:响应不能得到解决,或者不是一个领域

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

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

这是我用来执行搜索的code:

 私有静态最后弦乐PLACES_SEARCH_URL =?https://maps.googleapis.com/maps/api/place/search/json;

    私有静态最终布尔PRINT_AS_STRING = FALSE;


     公共无效performSearch()抛出异常{
      尝试 {
       的System.out.println(执行搜索......);
       的System.out.println(-------------------);
       HTT prequestFactory HTT prequestFactory = createRequestFactory(运输);
       HTT prequest请求= HTT prequestFactory.buildGetRequest(新GenericUrl(PLACES_SEARCH_URL));
       request.url.put(钥匙,API_KEY);
       request.url.put(位置,纬度+,+ LNG);
       request.url.put(半径,500);
       request.url.put(传感器,假);

       如果(PRINT_AS_STRING){
        的System.out.println(request.execute()parseAsString());
       } 其他 {

        。PlacesList地方= request.execute()parseAs(PlacesList.class);
        的System.out.println(STATUS =+ places.status);
        对于(地方的地方:places.results){
         的System.out.println(地方);
        }
       }

      }赶上(Htt的presponseException E){
       通信System.err.println(e.response.parseAsString());
       扔ê;
      }
}
 

解决方案

您可以通过使用的谷歌API的Java客户端 - 这是使用Java客户端获取所有的60结果的例子

 公共PlacesList搜索(双纬度,双经度,双半径,字符串类型)
            抛出异常{

        尝试 {

            HTT prequestFactory HTT prequestFactory = createRequestFactory(HTTP_TRANSPORT);
            HTT prequest请求= HTT prequestFactory
                    .buildGetRequest(新GenericUrl(https://maps.googleapis.com/maps/api/place/search/json?));
            。request.getUrl()把(钥匙,YOUR_API_KEY);
            。request.getUrl()把(位置,北纬+,+经度);
            。request.getUrl()把(半径,半径);
            。request.getUrl()把(传感器,假);
            。request.getUrl()把(类型,类型);

            。PlacesList列表= request.execute()parseAs(PlacesList.class);

            如果(list.next_page_token!= NULL || list.next_page_token!=){
                         视频下载(4000);
                         / *因为令牌可以后它已经产生一个很短的时间用* /
                。request.getUrl()把(pagetoken,list.next_page_token);
                。PlacesList温度= request.execute()parseAs(PlacesList.class);
                list.results.addAll(temp.results);

                如果(temp.next_page_token!= NULL || temp.next_page_token!=){
                    视频下载(4000);
                    。request.getUrl()把(pagetoken,temp.next_page_token);
                    PlacesList tempList = request.execute()parseAs(PlacesList.class)。
                    list.results.addAll(tempList.results);
              }

            }
            返回列表;

        }赶上(Htt的presponseException E){
            返回null;
        }

    }
 

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

    }

这篇关于谷歌API的地方 - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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