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

泪湿孤枕 提交于 2019-12-10 14:11:33

问题


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


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




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

}



回答3:


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


来源:https://stackoverflow.com/questions/10070347/android-how-to-to-set-imageview-as-a-marker-in-google-map-api-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!