Home > Enterprise >  How can I load an image saved to my Marker ID and display in a custom info window for that specific
How can I load an image saved to my Marker ID and display in a custom info window for that specific

Time:11-11

In my application I have the option to "long press" on a specific point on the map and then it display's a custom dialog window where one can put in a custom "Title" and "Snippet" and then take a photo and then save. This will then add the marker to the map.

Now this is where I am a bit stuck. I can get it to display the title and snippet for each marker but not the image taken for that specific marker.

I have tried saving the marker to a HashMap

private HashMap<Marker, String> markerImageID = new HashMap<Marker, String>();

And then add it to the drawMarker method:

private void drawMarker(LatLng location, String infoTitle, String infoSnippet, String img) {
    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(location);
    mMap.addMarker(new MarkerOptions()
            .title(infoTitle)
            .snippet(infoSnippet)
            .position(location)
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN)));
    markerImageID.put(marker, img);
}

Then when tapping on the map marker I use the following code:

@Override
        public View getInfoContents(Marker marker) {

            View v = getLayoutInflater().inflate(R.layout.info_window_layout, null);
            LatLng latLng = marker.getPosition();
            ImageView imgViewMarker = (ImageView) v.findViewById(R.id.imageViewFinal);
            TextView tvLat = (TextView) v.findViewById(R.id.txtMarkerTitle);
            TextView tvLng = (TextView) v.findViewById(R.id.txtMarkerSnippet);
            String filep = markerImageID.get(marker.getId());
            if (filep != null) {
                Bitmap myBitmap = BitmapFactory.decodeFile(img);
                imgViewMarker.setImageBitmap(myBitmap);
            }
            tvLat.setText(marker.getTitle());
            tvLng.setText(marker.getId());
            return v;
        }

But unfortunately this doesn't work as the image is not being displayed for that specific marker. The other information like the snippet, title and image is saved using SQLite database:

ContentValues contentValues = new ContentValues();
contentValues.put(LocationsDB.FIELD_LAT, point.latitude);
contentValues.put(LocationsDB.FIELD_LNG, point.longitude);
contentValues.put(LocationsDB.FIELD_ZOOM, mMap.getCameraPosition().zoom);
contentValues.put(LocationsDB.FIELD_TITLE, txtTitle.getText().toString());
contentValues.put(LocationsDB.FIELD_SNIPPET, txtSnippet.getText().toString());
contentValues.put(LocationsDB.FIELD_IMAGE_PATH, image.getAbsolutePath());

However when I just add the image as this:

View v = getLayoutInflater().inflate(R.layout.info_window_layout, null);
LatLng latLng = marker.getPosition();
ImageView imgViewMarker = (ImageView) v.findViewById(R.id.imageViewFinal);
TextView tvLat = (TextView) v.findViewById(R.id.txtMarkerTitle);
TextView tvLng = (TextView) v.findViewById(R.id.txtMarkerSnippet);
Bitmap myBitmap = BitmapFactory.decodeFile(img);
imgViewMarker.setImageBitmap(myBitmap);
tvLat.setText(marker.getTitle());
tvLng.setText(marker.getId());
return v;

It then displays the image in the ImageView, but always the last image that was taken and it then displays the same image for all the other markers that were added.

I then thought of using HashMap to save the Marker's id and then display the image that way but it's not working.

Hoping some one can assist me with this?

Thanks

CodePudding user response:

Instead of storing a hashmap containing for markers' images, how about setting a tag on the marker itself? See: https://developers.google.com/maps/documentation/android-sdk/reference/com/google/android/libraries/maps/model/Marker?hl=en

An Object associated with the marker. For example, the Object can contain data about what the marker represents. This is easier than storing a separate Map<Marker, Object>. As another example, you can associate a String ID corresponding to the ID from a data set. Google Maps SDK for Android neither reads nor writes this property.

You can set the String as a tag, or alternatively, you can create a model that wraps all the marker's data and set an instance of that model as the tag.

As far as the image not showing, are there any error messages you're seeing in the logs?

CodePudding user response:

I think you have a trivial error in drawMarker where you don't use the result of addMarker which is the added Marker object so you are associating the img to some previously assigned marker:

private void drawMarker(LatLng location, String infoTitle, String infoSnippet, String img) {
    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(location);

    // HERE: addMarker returns a Marker object and you never assign marker to it.  
    //       So I added the assignment

    marker = mMap.addMarker(new MarkerOptions()
            .title(infoTitle)
            .snippet(infoSnippet)
            .position(location)
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN)));

    // So in your original code the marker was never changing to the recently
    // added marker when you then associate it to the image.
    markerImageID.put(marker, img);
}

It is not clear from the code how the marker variable is used elsewhere (it's not local) so if necessary use a local variable to receive the Marker and use it in the put.

  • Related