【问题标题】:How do I get an Image from latitude and longitude using Google Places Api in Android?如何在 Android 中使用 Google Places Api 从纬度和经度获取图像?
【发布时间】:2020-01-28 02:16:42
【问题描述】:

我正在尝试使用 Google Place API 从纬度和经度信息中获取图像。这可能吗?

【问题讨论】:

  • 你能说得更具体一点吗?

标签: android google-places-api


【解决方案1】:

解决方案 1:

Use Geocoder for Reverse Geocoding.

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
try {
    List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
    StringBuilder sb = new StringBuilder();
    if (addresses.size() > 0) {
        Address address = addresses.get(0);
        for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
        // get Place id
    }catch(Exception e){
    }
}

解决方案 2: 获取地点 ID

https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=YOUR_API_KEY

然后您可以从特定地点 id 拍摄照片。

public void getPhotos(String placeId){


    // Specify fields. Requests for photos must always have the PHOTO_METADATAS field.
    List<Place.Field> fields = Arrays.asList(Place.Field.PHOTO_METADATAS);

    // Get a Place object (this example uses fetchPlace(), but you can also use findCurrentPlace())
    FetchPlaceRequest placeRequest = FetchPlaceRequest.newInstance(placeId, fields);

    placesClient.fetchPlace(placeRequest).addOnSuccessListener((response) -> {
        Place place = response.getPlace();

        // Get the photo metadata.
        PhotoMetadata photoMetadata = place.getPhotoMetadatas().get(0);

        // Get the attribution text.
        String attributions = photoMetadata.getAttributions();

        // Create a FetchPhotoRequest.
        FetchPhotoRequest photoRequest = FetchPhotoRequest.builder(photoMetadata)
                .setMaxWidth(500) // Optional.
                .setMaxHeight(300) // Optional.
                .build();
        placesClient.fetchPhoto(photoRequest).addOnSuccessListener((fetchPhotoResponse) -> {
            Bitmap bitmap = fetchPhotoResponse.getBitmap();
            imageView.setImageBitmap(bitmap);
        }).addOnFailureListener((exception) -> {
            if (exception instanceof ApiException) {
                ApiException apiException = (ApiException) exception;
                int statusCode = apiException.getStatusCode();
                // Handle error with given status code.
                Log.e(TAG, "Place not found: " + exception.getMessage());
            }
        });
    });
}

【讨论】:

  • 谢谢@John!地点 ID "INSERT_PLACE_ID_HERE" 会被 url 替换吗?
  • 不,您将从上述方法之一获得 PLACE_ID,然后您将通过它。
  • 我正在使用这个:getPhotos("maps.googleapis.com/maps/api/geocode/…KEY");这个可以吗?对不起,如果这个问题太基本了......刚刚开始
猜你喜欢
  • 2017-10-04
  • 2014-01-21
  • 1970-01-01
  • 2015-02-26
  • 1970-01-01
  • 1970-01-01
  • 2017-11-20
  • 2019-09-12
  • 2022-07-15
相关资源
最近更新 更多