前一段时间搞地图要显示周围房源信息,之前搜索的都是使用2.x的,现在百度地图官方新出了3.0版本由于之前思维局限一直没有实现图文并茂,今天看了别人2.0的实现方式,把它用到3.0上成功显示,下面看一下效果
现在3.0显示覆盖物mBaiduMap.addOverlay(OverlayOptions arg0),有两个类可以添加MarkerOptions和TextOptions,分别对应图片和文字,但是两个无法合在一起,换一个思路就是我们自定义覆盖物大多数都是自定义布局,但是查看官方文档没有现成的接口,所以我们可以把布局文件view转换成bitmap,然后通过BitmapDescriptorFactory.fromBitmap来获取BitmapDescriptor,这样就可以自定义图文并茂的覆盖物了,下面是如何将View转换成Bitmap的方法:
<span style="font-size:14px;">/** * 从view 得到图片 * @param view * @return */ public static Bitmap getBitmapFromView(View view) { view.destroyDrawingCache(); view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); view.setDrawingCacheEnabled(true); Bitmap bitmap = view.getDrawingCache(true); return bitmap; }</span>
<span style="font-size:14px;">@Override public void onGetSearchResult(CloudSearchResult result, int error) { if (result != null && result.poiList != null && result.poiList.size() > 0) { mBaiduMap.clear(); LatLng ll; BitmapDescriptor bd; LatLngBounds.Builder builder = new Builder(); for (CloudPoiInfo info : result.poiList) { TextView textView = new TextView(UElivesRentsRoom.this); textView.setGravity(Gravity.CENTER); textView.setBackgroundResource(R.drawable.icon_gcoding); textView.setTextColor(getResources().getColor(android.R.color.white)); ll = new LatLng(info.latitude, info.longitude); if (info.title != null) { textView.setText(info.title); }else { textView.setText("未知"); } bd = BitmapDescriptorFactory.fromBitmap(BMapUtil.getBitmapFromView(textView)); OverlayOptions oo = new MarkerOptions().icon(bd). position(ll); mBaiduMap.addOverlay(oo); builder.include(ll); bd.recycle(); } LatLngBounds bounds = builder.build(); MapStatusUpdate u = MapStatusUpdateFactory.newLatLngBounds(bounds); mBaiduMap.animateMapStatus(u); } }</span>
百度地图3.0实现图文并茂的覆盖物,布布扣,bubuko.com
原文:http://blog.csdn.net/jwzhangjie/article/details/36517719