Android: How to to set imageView as a marker in google map API android?
Asked Answered
U

3

6

till now i was using drawable to populate marker on my map .Now i was wondering it would be cool if i could display custom imageview as a marker in map.

till now i am doing it like

      itemized= new Itemazide(drawable, mContext);

i want to achieve something like

this

Unused answered 9/4, 2012 at 7:40 Comment(0)
C
1

Yeah I was wondering if I could do something like this, i.e. show a custom View instead of drawable. You can override draw() method, but unfortunately it always (someone please tell me I'm wrong) has to be drawable. I think it's because custom View would take too much memory.

That being said, depending on what you're trying to achieve it's probably possible to hack mapview-baloon library to achieve some similar effect.

EDIT: Now that you've shown what you're trying to achieve I think you should override draw() in your OverlayItem and in this method inflate your ImageView. But I didn't try to do it this way, so there may be some drawbacks (I remember a SO thread on a similar matter that claimed, that it would interrupt all touch events on this OverlayItem).

Covered answered 9/4, 2012 at 7:57 Comment(2)
mapview-baloon its different thingUnused
It allows you to inflate whatever View (including custom ImageView). But not instead of Drawable, rather over it (but you can always use transparent drawable). And I think it shouldn't be that hard to hack it, so it doesn't show onTap, but rather always.Butterfish
M
2

I might be a bit late but I'll post a solution for others who have faced/are facing a similar issue. So basically what you have to do (at least for the solution you are seeking, i.e a custom image imposed on a box-like background) is impose the customImage on the background box with the help of a canvas. Using this implementation you can effectively create a BitmapDrawable from the canvas that you can then assign as a marker for your 'Overlay' / 'ItemizedOverlay'. Also, please refrain from creating an ImageView for each overlay as this will utterly destroy your memory/ your app if you have to deal with thousands of such ImageViews simultaneously. Instead, use BitmapDrawables that can be assigned to the overlays during their construction and don't consume nearly enough memory as an ImageView.

public BitmapDrawable imageOnDrawable(int drawableBackground, Bitmap customImage)
{
    //The following line is optional but I'd advise you to minimize the size of 
    //the size of the bitmap (using a thumbnail) in order to improve draw
    //performance of the overlays (especially if you are creating a lot of overlays).

    Bitmap customImageThumbnail = ThumbnailUtils.extractThumbnail(
                                                    customImage, 100, 100); 

    Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId);
    bm = Bitmap.createScaledBitmap(bm, 112, 120, false);

    Canvas canvas = new Canvas(bm);
    canvas.drawBitmap(bm, 0, 0, null);
    // The 6,6 in the below line refer to the offset of the customImage/Thumbnail
    // from the top-left corner of the background box (or whatever you want to use
    // as your background) 
    canvas.drawBitmap(customImageThumbnail, 6, 6, null); 

    return new BitmapDrawable(bm);

}
Marder answered 9/7, 2012 at 18:55 Comment(0)
C
1

Yeah I was wondering if I could do something like this, i.e. show a custom View instead of drawable. You can override draw() method, but unfortunately it always (someone please tell me I'm wrong) has to be drawable. I think it's because custom View would take too much memory.

That being said, depending on what you're trying to achieve it's probably possible to hack mapview-baloon library to achieve some similar effect.

EDIT: Now that you've shown what you're trying to achieve I think you should override draw() in your OverlayItem and in this method inflate your ImageView. But I didn't try to do it this way, so there may be some drawbacks (I remember a SO thread on a similar matter that claimed, that it would interrupt all touch events on this OverlayItem).

Covered answered 9/4, 2012 at 7:57 Comment(2)
mapview-baloon its different thingUnused
It allows you to inflate whatever View (including custom ImageView). But not instead of Drawable, rather over it (but you can always use transparent drawable). And I think it shouldn't be that hard to hack it, so it doesn't show onTap, but rather always.Butterfish
T
1
In google mapv2, map-view ballon like functionality is provided by default.
Just, you have to write some lines for it :

private GoogleMap mapView;

if (mapView == null)
            mapView = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map_view)).getMap();

        mapView.getUiSettings().setMyLocationButtonEnabled(false);
        mapView.setMyLocationEnabled(true);

MarkerOptions markerDestinationOptions;
markerDestinationOptions = new MarkerOptions()
                    .position(latLngDestination)                .icon(BitmapDescriptorFactory.fromResource(R.drawable.marker));

mapView.addMarker(markerDestinationOptions);
Tricyclic answered 27/11, 2015 at 7:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.