我在春天寫了下面的方法來獲取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應用程序,以顯示谷歌的地方圖片,您需要執行以下操作:
首先在Android中獲取PlaceID。在我的情況下,我通過我的android應用上的AutoCompleteTextView獲取了我的PlaceID:(https://developers.google.com/places/android/autocomplete)(Call 1)
然後我打電話給我的方法如下。我致電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是否仍然有效?
Panaramio實際上是關閉反正遷移到谷歌瀏覽:https://groups.google.com/forum/#!topic/panoramio-questions-support/R5toz0EAB8k所以最好不要使用。 – Simon
嗨西蒙,你有沒有找到更好的解決方案,無需3次電話拍照? – Zookey
對不起。老實說,我不認爲谷歌使他們的數據友好的移動設備。 – Simon