首页 > 解决方案 > 如何获取从相机拍摄的图像并将其添加为谷歌地图上的标记?

问题描述

好的,所以我要做的是长按我的地图,然后使用相机意图拍照,然后将该照片作为用户“长按”的地图点上的标记返回

有效的是长按然后拍照。不起作用的是将该图像作为标记返回到地图。

这就是我用来添加标记并使用相机拍摄图像的方法:

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    LatLng mylocation = new LatLng(-xx.xxxx, xx.xxxx);
    mMap.addMarker(new MarkerOptions().position(mylocation).title("Marker in mylocation"));
    CameraPosition cameraPosition = new CameraPosition.Builder().target(mylocation).zoom(12).build();
    mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    googleMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {

        @Override
        public void onMapLongClick(LatLng latLng) {
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
            root = Environment.getExternalStorageDirectory().toString() + "/My Folder";
            imageFolderPath = root + "/saved_images";
            File imagesFolder = new File(imageFolderPath);
            imagesFolder.mkdirs();
            imageName = "test.png";
            File image = new File(imageFolderPath, imageName);
            fileUri = Uri.fromFile(image);
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
            startActivityForResult(takePictureIntent, TAKE_PICTURE);
        }
        public void onActivityResult(int requestCode, int resultCode, Intent data, LatLng latlng) {
            if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK) {

                try {
                    GetImageThumbnail getImageThumbnail = new GetImageThumbnail();
                    bitmap = getImageThumbnail.getThumbnail(fileUri, this);
                } catch (FileNotFoundException e1) {
                    e1.printStackTrace();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }

                MarkerOptions markerOptions = new MarkerOptions()
                        .position(latlng)
                        .icon(BitmapDescriptorFactory.fromBitmap(bitmap));
                googleMap.addMarker(markerOptions);
            }
        }
    });

这是 GetImageThumbnail 类:

public class GetImageThumbnail {

private static int getPowerOfTwoForSampleRatio(double ratio) {
    int k = Integer.highestOneBit((int) Math.floor(ratio));
    if (k == 0)
        return 1;
    else
        return k;
}

public Bitmap getThumbnail(Uri uri, GoogleMap.OnMapLongClickListener test)
        throws FileNotFoundException, IOException {
    InputStream input = ((Context) test).getContentResolver().openInputStream(uri);

    BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
    onlyBoundsOptions.inJustDecodeBounds = true;
    onlyBoundsOptions.inDither = true;// optional
    onlyBoundsOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional
    BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
    input.close();
    if ((onlyBoundsOptions.outWidth == -2)
            || (onlyBoundsOptions.outHeight == -2))
        return null;

    int originalSize = Math.max(onlyBoundsOptions.outHeight, onlyBoundsOptions.outWidth);

    double ratio = (originalSize > 200) ? (originalSize / 175) : 0.5;

    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
    bitmapOptions.inSampleSize = getPowerOfTwoForSampleRatio(ratio);
    bitmapOptions.inDither = true;
    bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional
    input = ((Context) test).getContentResolver().openInputStream(uri);
    Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
    input.close();
    return bitmap;
  }
}

正如我提到的相机意图启动并且我可以拍照,但是在拍照后它返回到地图但没有标记?也没有错误消息或任何我能找到的关于它为什么不显示标记/图像的信息。

我确定我在这里做错了什么,但不知道是什么?

谢谢

标签: javaandroidgoogle-maps

解决方案


推荐阅读