Android:如何在google map API中将imageView设置为标记android?

问题描述:

到现在为止我使用drawable在我的地图上填充标记。现在我想知道如果我可以在地图中显示自定义的imageview作为标记,它将会很酷。Android:如何在google map API中将imageView设置为标记android?

到现在我正在做它像

 itemized= new Itemazide(drawable, mContext); 

我想要实现像

this

是的,我在想,如果我可以做这样的事情,即显示自定义View而不是可绘制的。您可以覆盖draw()方法,但不幸的是它总是(有人告诉我我错了)必须是可绘制的。我想这是因为自定义View会占用太多内存。

这就是说,取决于你试图达到的目标,它可能会破解mapview-baloon库来实现一些类似的效果。

编辑: 现在你已经出你想要达到什么样的,我认为你应该重写你的OverlayItemdraw()在这个方法虚增您ImageView。但我没有试图这样做,所以可能有一些缺点(我记得一个类似的问题上的SO线程,声称它会中断这个OverlayItem上的所有触摸事件)。

+0

我一直在寻找这个,现在我想实现让我们看看... – Akram 2012-04-09 08:12:29

+0

MapView的-气球其不同的事情 – Akram 2012-04-09 10:03:24

+0

它可以让你无论膨胀'View'(包括自定义'ImageView') 。但不是**而是'Drawable',而不是它(但你总是可以使用透明绘图)。我认为它不应该很难入侵它,所以它不会在TAP上显示,而是始终显示。 – 2012-04-09 10:24:22

我可能会有点晚,但我会为其他遇到/正面临类似问题的人发布解决方案。所以基本上你必须做的事情(至少对于你正在寻找的解决方案,即强加在盒状背景上的定制图像)是通过画布将customImage强加在背景框上。使用此实现,您可以从画布中有效地创建一个BitmapDrawable,然后您可以将其指定为“Overlay”/“ItemizedOverlay”的标记。 另外,请不要为每个叠加层创建一个ImageView,因为如果必须同时处理数千个这样的ImageView,这将彻底摧毁您的内存/您的应用程序。相反,使用可在构建过程中分配给叠加层的BitmapDrawable,并且不会像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); 

} 

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