问题描述
我在春季编写了以下方法,以获取今天早上的Google Places照片。 该方法仍然存在问题-对于可以修复代码的人有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;
}
我认为要获取一个可显示Google Places图片的android应用,必须执行以下操作:
首先在Android中获取PlaceID。 就我而言,我是通过Android应用程序上的AutoCompleteTextView获取了PlaceID的:( )(电话1)
然后,我在下面调用我的方法。 我调用Google Places API以获取位置详细信息(调用2),然后在详细信息返回后,我使用Jackson解析出photo_reference并再次调用Google Places API以将照片作为位图返回(调用3)。 。
我正在打3次电话给Google地方信息以返回照片。 与每天1000个呼叫的配额相比,要获得一张照片,这是相当可观的呼叫数量。
在没有打那么多电话的情况下,还有其他更少的方法来获取照片吗?
我看了一下这个线程:
该人建议人们改用panaramio,这在一开始似乎是一个非常不错的选择,但是当我通过在浏览器中输入示例进行测试时,它是: : ,.php文件中没有返回照片。
我不确定panaramio API是否仍然有效?
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_reference
是photo
数组元素中的String值
另外,以下是不必要的工作:
//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;
}
}
有关完整的示例,请参见:源代码可下载。