百度地图3.0实现图文并茂的覆盖物

前一段时间搞地图要显示周围房源信息,之前搜索的都是使用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>

我做的是云检索显示内容,详细的能够查看官方的demo,我这里仅仅写出用到的主要函数的方法:

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

我上面是使用TextView,假设要显示其它的内容自己能够使用布局文件。

原文地址:https://www.cnblogs.com/lcchuguo/p/4013044.html