HashMap不会通过onMarkerClick将图像返回到标记

时间:2014-12-11 16:00:26

标签: android google-maps

我有一个由相机意图创建的标记,然后以缩略图的形式返回到地图。现在我想要做的是点击标记以显示添加的每个标记的完整图像。以前我可以全屏显示图像,但它只显示所有标记的最后一张图像,而不显示每个标记的单个图像。

这是我的代码:

Bitmap bitmap;
private String TAG;
File destinationFile;
public static final int CAPTURE_IMAGE_THUMBNAIL_ACTIVITY_REQUEST_CODE = 1888;
private Map<File, String> markerImagePathMap;


@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_googlemaps);
        markerImagePathMap = new HashMap<File, String>();


  try {
        initilizeMap();

    } catch (Exception e) {
        e.printStackTrace();
    }
    }


private void initilizeMap() {
    if (googleMap == null) {
        googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

        if (googleMap == null) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}

@Override
    public void onResume() {
    super.onResume();
    initilizeMap();

    if (googleMap!=null){
      }

 CameraPosition cameraPosition = new CameraPosition.Builder().target(
         new LatLng(xx.xxxx, xx.xxxx)).zoom(9).bearing(0).tilt(80).build();

 googleMap.setOnMapClickListener(this);
 googleMap.setOnMapLongClickListener(this);
 googleMap.setOnMarkerClickListener(this);
 googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
 googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
 googleMap.setMyLocationEnabled(true);
 googleMap.getUiSettings().setZoomControlsEnabled(false);
 googleMap.getUiSettings().setCompassEnabled(true);
 googleMap.getUiSettings().setMyLocationButtonEnabled(true);
}


public void onMapLongClick(final LatLng point) {

      googleMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

          @Override
          public void onInfoWindowClick(Marker marker) {
              marker.remove();
            }
      });
      Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
      startActivityForResult(intent, CAPTURE_IMAGE_THUMBNAIL_ACTIVITY_REQUEST_CODE);
  }

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{ 
    if (requestCode == CAPTURE_IMAGE_THUMBNAIL_ACTIVITY_REQUEST_CODE)
    {
        File imageStorageFolder = new File(Environment.getExternalStorageDirectory()+File.separator+"My Folder");
        if (!imageStorageFolder.exists())
        {
            imageStorageFolder.mkdirs();
            Log.d(TAG , "Folder created at: "+imageStorageFolder.toString());
        }
        if (data != null)
        {
            String filename = "image";
            String fileNameExtension = ".jpg";
            File sdCard = Environment.getExternalStorageDirectory();
            String imageStorageFolder1 = File.separator+"My Folder"+File.separator;
            destinationFile = new File(sdCard, imageStorageFolder1 + filename + fileNameExtension);
            Log.d(TAG, "the destination for image file is: " + destinationFile );
            if (data.getExtras() != null)
            {
                bitmap = (Bitmap)data.getExtras().get("data");
                try
                {
                    FileOutputStream out = new FileOutputStream(destinationFile);
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                    out.flush();
                    out.close();
                }
                catch (Exception e)
                {
                    Log.e(TAG, "ERROR:" + e.toString());
                }
                MarkerOptions markerOptions = new MarkerOptions()
                            .draggable(true)
                            .snippet("Snippet")
                            .title("Title")
                            .position(position)
                            .icon(BitmapDescriptorFactory
                                    .fromBitmap(bitmap));
                            googleMap.addMarker(markerOptions);
                            Marker marker = googleMap.addMarker(markerOptions);
                            markerImagePathMap.put(destinationFile, marker.getId());
            }
        }
    }
 }
        public void onMapClick (LatLng point){
    }
 {
 }
    @Override
        public boolean onMarkerClick (Marker marker){

        markerImagePathMap.put(destinationFile, marker.getId());         
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            markerImagePathMap.put(destinationFile, marker.getId());
            Uri imgUri = Uri.parse("file://" + markerImagePathMap.get(marker.getId()));
            intent.setDataAndType(imgUri, "image/*");
            startActivity(intent);
          return false;
  }
 }

但问题是,当点击标记时它没有显示任何图像,只显示此图像(文件未找到或损坏或其他类似的效果)

enter image description here

1 个答案:

答案 0 :(得分:0)

如果你想这样做:

Uri imgUri = Uri.parse("file://" + markerImagePathMap.get(marker.getId())

你需要这样做:

markerImagePathMap.put(marker.getId(), destinationFile);

所以代码应该是这样的:

private Map<String, File> markerImagePathMap;

protected void onCreate(Bundle savedInstanceState) {
    ...
    markerImagePathMap = new HashMap<String, File>();
    ...
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    ...
    //destinationFile = new File(sdCard, imageStorageFolder1 + filename + fileNameExtension);  // <--- Delete
    File destinationFile = new File(sdCard, imageStorageFolder1 + filename + fileNameExtension);  // <--- Add
    ...
    Marker marker = googleMap.addMarker(markerOptions);
    Log.d(TAG, marker.getId() + " -> " + destinationFile.getAbsolutePath());   // <--- Add
    markerImagePathMap.put(marker.getId(), destinationFile);
    ...
}

public boolean onMarkerClick (Marker marker){
    ...
    //markerImagePathMap.put(destinationFile, marker.getId());  <-- what is this for?
    File destinationFile = markerImagePathMap.get(marker.getId())
    Uri imgUri = Uri.parse("file://" + destinationFile.getAbsolutePath());
    ...
}