2012-01-26 117 views
0

我正在製作一個android應用程序,需要在我10km以內的本地區域進行搜索,並使用引腳將結果顯示在地圖上,例如:「Starbucks」,「Wallmart」,Shopping mall等。我在我的活動類中指定的搜索詞。並且要清楚:我不想在Google地圖中打開搜索,我希望它能在我自己的應用程序中顯示結果。但是我在執行搜索的代碼中出現錯誤。錯誤出現在以下東西:Google Places API - android

網址:網址不能得到解決或不是一個場 執行:該方法執行()是未定義的類型的HttpRequest 響應:響應不能得到解決或不是一個場

我使用三個包: com.mycompany.applicationname =默認包,其中包含主代碼,包括搜索碼 com.mycompany.applicationname.Model =含PlaceAutoComplete,PlacesList,地點等 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; 
     } 
} 

回答

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

    }