Android - 获取Google商家信息照片和API调用

时间:2015-07-19 09:52:36

标签: java android spring google-places-api

我今年早上在Spring编写了下面的方法来获取Google Places Photo。这种方法仍然是错误的 - 对于可以修复代码的人来说只有10分 - 但它显示了我想要做的事情的要点:

  @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应用显示Google商家信息图片,必须执行以下操作:

  1. 首先在Android中获取PlaceID。在我的情况下,我通过我的Android应用程序上的AutoCompleteTextView获取了我的PlaceID:(https://developers.google.com/places/android/autocomplete)(调用1)

  2. 然后我打电话给我的方法。我调用Google Places API获取地方详细信息(第2部分),然后在详细信息返回后,我使用杰克逊解析照片参考并再次调用Google Places API以将照片作为位图等返回(致电3)

  3. 我正在拨打3个Google地方的电话来退回照片。与每天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是否仍有效?

1 个答案:

答案 0 :(得分: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/