Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 582
Akram Avatar asked Apr 09 '12 07:04

Akram


People also ask

How do I add a marker on Google Maps Android?

For adding a custom marker to Google Maps navigate to the app > res > drawable > Right-Click on it > New > Vector Assets and select the icon which we have to show on your Map. You can change the color according to our requirements. After creating this icon now we will move towards adding this marker to our Map.

How do I start a custom image on Google Maps marker for mobile app?

You can start by looking at the Canvas and Drawables from the Android Developer page. Now you also want to download a picture from an URL. URL url = new URL(user_image_url); HttpURLConnection conn = (HttpURLConnection) url. openConnection(); conn.

How do I add a custom marker to Google Maps?

Once you are on the map creation page, click the marker icon to add a marker to the page. Find a place on the map you want to add a marker and click on that location on the map. This will place a marker on the map and bring up a box where you can add a name, description, and other attributes to the marker.


3 Answers

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);

}
like image 74
Vishwa Patel Avatar answered Oct 20 '22 07:10

Vishwa Patel


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).

like image 29
Michał Klimczak Avatar answered Oct 20 '22 07:10

Michał Klimczak


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);
like image 1
Harsh Parikh Avatar answered Oct 20 '22 07:10

Harsh Parikh