2013-08-01 44 views
1

我一直在試圖提取GOOLGE Places API的照片參考,但都沒有成功。我想知道是否有人可以幫助我。下面是我的代碼:谷歌Places API的photo_reference

// KEY Strings 
public static String KEY_REFERENCE = "reference"; // id of the place 
public static String KEY_NAME = "name"; // name of the place 
public static String KEY_VICINITY = "vicinity"; // Place area name 
public static String KEY_PHOTO = "photo_reference"; 

class LoadPlaces extends AsyncTask<String, String, String> { 

    /** 
    * getting google places JSON response 
    * */ 

    protected String doInBackground(String... args) { 
     // creating Places class object 
     googlePlaces = new GooglePlaces(); 

     try { 
      String types = MenuActivity.type; 
      String keyword = MenuActivity.keyword; 

      // get nearest places 
      nearPlaces = googlePlaces.search(gps.getLatitude(),gps.getLongitude(), 
      types, keyword); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    protected void onPostExecute(String file_url) { 
     // updating UI from Background Thread 

     runOnUiThread(new Runnable() { 
      public void run() { 
       /** 
       * Updating parsed Places into LISTVIEW 
       * */ 

       // Get JSON response status 
       String status = nearPlaces.status; 

       // Check for OK status 
       if (status.equals("OK")) { 
        // Successfully got places details 
        if (nearPlaces.results != null) { 
         // loop through each place 
         for (Place p : nearPlaces.results) { 
          HashMap<String, String> map = new HashMap<String, String>(); 

          map.put(KEY_REFERENCE, p.reference); 
          map.put(KEY_NAME, p.name); 
          map.put(KEY_PHOTO,p.photo); 
          map.put(KEY_VICINITY, p.vicinity); 

          // adding HashMap to ArrayList 
          placesListItems.add(map); 

         } 
         // list adapter - removed rating 
         ListAdapter adapter = new SimpleAdapter(
           MainActivity.this, placesListItems, 
           R.layout.list_item, new String[] { 
           KEY_REFERENCE, KEY_NAME, KEY_VICINITY, KEY_PHOTO}, 
           new int[] { R.id.reference, R.id.name, R.id.address, R.id.phptp}); 

         // Adding data into ListView 
         lv.setAdapter(adapter); 
        } 
       } 
} 

下面是我的代碼,執行搜索和分析數據:

public class GooglePlaces { 

/** Global instance of the HTTP transport. */ 
private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport(); 
private static final String LOG_KEY = "GGPlace"; 

// Google API Key 
private static final String API_KEY = ""; 

// Google Places serach 
private static final String PLACES_SEARCH_URL = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?&rankby=distance"; 

private double _latitude; 
private double _longitude; 
private double _radius; 
private String address; 

public PlacesList search(double latitude, double longitude, String types, String keyword) 
     throws Exception { 

    this._latitude = latitude; 
    this._longitude = longitude; 

    try { 

     HttpRequestFactory httpRequestFactory = createRequestFactory(HTTP_TRANSPORT); 
     HttpRequest request = httpRequestFactory.buildGetRequest(new GenericUrl(PLACES_SEARCH_URL)); 
     request.getUrl().put("key", API_KEY); 
     request.getUrl().put("location", _latitude + "," + _longitude); 
     request.getUrl().put("sensor", "true"); 
     if(types != null) 
     { 
      request.getUrl().put("types", types); 
      request.getUrl().put("keyword", keyword); 
     } 

     PlacesList list = request.execute().parseAs(PlacesList.class); 
     // Check log cat for places response status 
     Log.d("Places Status", "" + list.status); 
     return list; 

    } catch (HttpResponseException e) { 
     Log.e("Error:", e.getMessage()); 
     return null; 
    } 

} 
public static HttpRequestFactory createRequestFactory(
     final HttpTransport transport) { 
    return transport.createRequestFactory(new HttpRequestInitializer() { 
     public void initialize(HttpRequest request) { 
      GoogleHeaders headers = new GoogleHeaders(); 
      headers.setApplicationName("APP NAME"); 
      headers.gdataVersion="2"; 
      request.setHeaders(headers); 
      JsonHttpParser parser = new JsonHttpParser(new JacksonFactory()); 
      request.addParser(parser); 
     } 
    }); 
} 
} 

這是我PlaceList類:

public class PlacesList implements Serializable { 
@Key 
public String status; 

@Key 
public List<Place> results; 

} 

這裏是我的地方類:

public class Place implements Serializable { 

@Key 
public String id; 

@Key 
public String name; 

@Key 
public String reference; 

@Key 
public String vicinity; 

@Key 
public Geometry geometry; 

@Key 
public List<Photo> photos; 
} 

最後我的照片分類:

public class Photo implements Serializable { 

@Key 
public String photo_reference; 

@Key 
public int height; 

@Key 
public int width; 

} 

我想我打電話或通過錯誤的方式傳遞photo_reference。我希望有人能幫助我。我已經爲此工作了幾個星期,幾乎完全放棄了。

+0

這可能不是問題,但使用的是'runOnUiThread'在'onPostExecute()'。由於'onPostExecute()'在UI線程上運行,因此這不是必需的。 – Vikram

+0

我想我的問題還真是,谷歌Places API的的photo_reference與結果數組的數組,我不知道如何訪問並正確解析它。 –

+0

如果你想用一個庫來處理此分析對您(或只是查看源代碼,看看它是如何正在做),你可以檢查出[鏈輪(https://github.com/pushbit/sprockets) (披露:我是開發人員)。搜索結果被分解成與您已經使用的類非常相似的類。而且還有對其他谷歌Places API的服務(詳細信息,照片等) – pushbit

回答

0

你好,首先你的搜索網址是錯誤的。

你必須遵循以下格式:

https://developers.google.com/places/web-service/photos

請參見下面的完整的例子:

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

如果你下載的源代碼,它會幫助您瞭解在另一個數組中的數組中獲取json字符串。

的片段下面只是回答,你必須獲取圖像的一部分:

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; 
    } 
} 
相關問題