Baidu MAP 3.0 implements Graphic Overlay

Source: Internet
Author: User

tag: des Android style blog HTTP color Io OS usage

前一段时间搞地图要显示周围房源信息,之前搜索的都是使用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;">@Overridepublic 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,假设要显示其它的内容自己能够使用布局文件。

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.