2015-07-19 79 views
1

我在春天寫了下面的方法來獲取Google Places照片。該方法仍是越野車 - 10分的人誰可以修復了代碼 - 但它表明的是我想做的要點:Android - 獲取Google Places照片和API調用

@RequestMapping(method=RequestMethod.GET, value="/placedetails") 
    public BufferedImage PlaceDetails(@PathVariable String placeid) { 
     ArrayList<String> placePhotos = new ArrayList<>(); 

     OkHttpClient client = new OkHttpClient(); 
     Request request = new Request.Builder() 
      .url("https://maps.googleapis.com/maps/api/place/details/json?placeid="+placeid+"&key="+serverKey) 
      .build(); 

     try { 
      //calling the GoogleAPI to get the PlaceDetails so that I can extract the photo_reference 
      Response response = client.newCall(request).execute(); 
      //parsing the response with Jackson so that I can get the photo_reference 
      ObjectMapper m = new ObjectMapper(); 
      JsonNode rootNode = m.readTree(response.body().string()); 
      JsonNode resultNode = rootNode.get("result"); 
      final JsonNode photoArrayNode = resultNode.get("photos"); 
      if (photoArrayNode.isArray()) { 
       for (JsonNode photo: photoArrayNode) { 
        placePhotos.add(photo.get("photo_reference").textValue()); 
       } 
      } 
      //calling the GoogleAPI again so that I can get the photoUrl 
      String photoUrl = String.format("https://maps.googleapis.com/maps/api/place/photo?maxwidth=%s&photoreference=%s&key=%s", 
        400, 
        placePhotos.get(0), 
        serverKey); 
      System.out.println(photoUrl); 

      //getting the actual photo 
      Request photoRequest = new Request.Builder().url(photoUrl).build(); 
      Response photoResponse = client.newCall(photoRequest).execute(); 
      if (!photoResponse.isSuccessful()) throw new IOException("Unexpected code " + response); 
      //returning the photo 
      return ImageIO.read(photoResponse.body().byteStream()); 

     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return null; 
    } 

我想獲得一個Android應用程序,以顯示谷歌的地方圖片,您需要執行以下操作:

  1. 首先在Android中獲取PlaceID。在我的情況下,我通過我的android應用上的AutoCompleteTextView獲取了我的PlaceID:(https://developers.google.com/places/android/autocomplete)(Call 1)

  2. 然後我打電話給我的方法如下。我致電Google Places API獲取地點詳細信息(Call 2),然後一旦細節返回,我使用Jackson解析出photo_reference並再次調用Google Places API以將照片作爲位圖返回等等。(Call 3) 。

我正在對Google Places進行3次調用以返回照片。與每天1000個電話的配額相比,這是相當數量的獲取1張照片的電話。

有沒有其他方法可以讓照片沒有太多的電話?

我看着這個線程:How to get a picture of a place from google maps or places API

的人認爲,一個使用panaramio,而不是這似乎是在開始一個很好的選擇,但是當我測試了它在我的瀏覽器中的示例鍵入:http://www.panoramio.com/map/get_panoramas.php?set=public&from=0&to=20&minx=-33.868&miny=151.193&maxx=-33.864&maxy=151.197&size=medium&mapfilter=true ,.php文件中沒有返回照片。

我不確定panaramio API是否仍然有效?

+0

Panaramio實際上是關閉反正遷移到谷歌瀏覽:https://groups.google.com/forum/#!topic/panoramio-questions-support/R5toz0EAB8k所以最好不要使用。 – Simon

+0

嗨西蒙,你有沒有找到更好的解決方案,無需3次電話拍照? – Zookey

+0

對不起。老實說,我不認爲谷歌使他們的數據友好的移動設備。 – Simon

回答

1

你好你的問題是在這裏

  if (photoArrayNode.isArray()) { 
       for (JsonNode photo: photoArrayNode) { 
        placePhotos.add(photo.get("photo_reference").textValue()); 
       } 

  if (photoArrayNode.isArray()) { 
       for (JsonNode photo: photoArrayNode) { 
        placePhotos.add(photo.get("photo_reference").getString()); 
       } 

photo_referencephoto數組中的字符串值

此外,以下是不必要的工作:

//calling the GoogleAPI again so that I can get the photoUrl 
      String photoUrl = String.format("https://maps.googleapis.com/maps/api/place/photo?maxwidth=%s&photoreference=%s&key=%s", 

不需要格式化url字符串。下面的代碼片段是我在下面推薦的例子的一部分,它專門回答你的問題。

package in.wptrafficanalyzer.locationnearbyplacesphotos; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.util.Log; 

public class PlaceJSONParser { 

    /** Receives a JSONObject and returns a list */ 
    public Place[] parse(JSONObject jObject){  

     JSONArray jPlaces = null; 
     try {   
      /** Retrieves all the elements in the 'places' array */ 
      jPlaces = jObject.getJSONArray("results"); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     /** Invoking getPlaces with the array of json object 
     * where each json object represent a place 
     */ 
     return getPlaces(jPlaces); 
    } 


    private Place[] getPlaces(JSONArray jPlaces){ 
     int placesCount = jPlaces.length();  
     Place[] places = new Place[placesCount];  

     /** Taking each place, parses and adds to list object */ 
     for(int i=0; i<placesCount;i++){ 
      try { 
       /** Call getPlace with place JSON object to parse the place */ 
       places[i] = getPlace((JSONObject)jPlaces.get(i));    


      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 

     return places; 
    } 

    /** Parsing the Place JSON object */ 
    private Place getPlace(JSONObject jPlace){ 

     Place place = new Place(); 



     try { 
      // Extracting Place name, if available 
      if(!jPlace.isNull("name")){    
       place.mPlaceName = jPlace.getString("name"); 
      } 

      // Extracting Place Vicinity, if available 
      if(!jPlace.isNull("vicinity")){ 
       place.mVicinity = jPlace.getString("vicinity"); 
      } 

      if(!jPlace.isNull("photos")){ 
       JSONArray photos = jPlace.getJSONArray("photos"); 
       place.mPhotos = new Photo[photos.length()]; 
       for(int i=0;i<photos.length();i++){ 
        place.mPhotos[i] = new Photo(); 
        place.mPhotos[i].mWidth = ((JSONObject)photos.get(i)).getInt("width"); 
        place.mPhotos[i].mHeight = ((JSONObject)photos.get(i)).getInt("height"); 
        place.mPhotos[i].mPhotoReference = ((JSONObject)photos.get(i)).getString("photo_reference"); 
        JSONArray attributions = ((JSONObject)photos.get(i)).getJSONArray("html_attributions"); 
        place.mPhotos[i].mAttributions = new Attribution[attributions.length()]; 
        for(int j=0;j<attributions.length();j++){ 
         place.mPhotos[i].mAttributions[j] = new Attribution(); 
         place.mPhotos[i].mAttributions[j].mHtmlAttribution = attributions.getString(j); 
        }     
       } 
      } 

      place.mLat = jPlace.getJSONObject("geometry").getJSONObject("location").getString("lat"); 
      place.mLng = jPlace.getJSONObject("geometry").getJSONObject("location").getString("lng");      



     } catch (JSONException e) {   
      e.printStackTrace(); 
      Log.d("EXCEPTION", e.toString()); 
     }  
     return place; 
    } 
} 

有關完整的示例,請參閱:源代碼可供下載。

http://wptrafficanalyzer.in/blog/showing-nearby-places-with-photos-at-any-location-in-google-maps-android-api-v2/