百度地图多个InfoWindow同时展示

来源:互联网 发布:什么英语听力软件最好 编辑:程序博客网 时间:2024/05/01 06:45

由于百度的InfoWindow只能展示一个,marker却不受限制,因此换了一个思路,在网上搜集了许多资料,最终实验成功。

通过创建一个自定义的bitmap然后以marker的形式添加到地图上。


在xml中,只有一个地图控件,因为手边没有真机,就用模拟器进行测试的,地图是用的是TextureMapView使用默认的MapVIew会报错。

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.pistol.markerinfos.MainActivity">    <com.baidu.mapapi.map.TextureMapView        android:id="@+id/bmapView"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:clickable="true" /></RelativeLayout>
activity内容如下图:

public class MainActivity extends AppCompatActivity {    private TextureMapView bmapView;    private BaiduMap mBaiduMap;    private BitmapDescriptor mIconMarker;    private Bitmap bitmap = null;    private List<LatLng> latList = new ArrayList<>();    // 天安门坐标    private double mLat1 = 39.915291;    private double mLon1 = 116.403857;    // 百度大厦坐标    private double mLat2 = 40.056858;    private double mLon2 = 116.308194;    // 西湖坐标    private double mLat3 = 30.2446230000;    private double mLon3 = 120.1517040000;    private LatLng loc_start;    private LatLng loc_end;    private LatLng loc_position;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //在使用SDK各组件之前初始化context信息,传入ApplicationContext        //注意该方法要再setContentView方法之前实现        SDKInitializer.initialize(getApplicationContext());        setContentView(R.layout.activity_main);        initView();        initData();    }    private void initView() {        bmapView = (TextureMapView) findViewById(R.id.bmapView);        mBaiduMap = bmapView.getMap();        mIconMarker = BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher);    }    int i = 0;    private void initData() {        loc_start = new LatLng(mLat1, mLon1);        loc_end = new LatLng(mLat2, mLon2);        loc_position = new LatLng(mLat3, mLon3);        latList.add(loc_start);        latList.add(loc_end);        latList.add(loc_position);        for (LatLng position : latList) {            i++;            showMarker(position, "第" + i + "个框");        }        updateMap();    }    private void updateMap() {        mBaiduMap.setOnMapLoadedCallback(new BaiduMap.OnMapLoadedCallback() {            @Override            public void onMapLoaded() {                //仿照zoomToSpan()写的自适应                LatLngBounds.Builder builder = new LatLngBounds.Builder();                for (int i = 0; i < latList.size(); i++) {                    builder.include(latList.get(i));                }                //更新地图                mBaiduMap.animateMapStatus(MapStatusUpdateFactory                        .newLatLngBounds(builder.build()));            }        });    }    private void showMarker(LatLng latLng, String str) {        // 创建bitmap        drawBitMap(str);        OverlayOptions option = new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.fromBitmap(bitmap));        if (mBaiduMap != null) {            mBaiduMap.addOverlay(option);        }        gc();    }    private void drawBitMap(String str) {        float scale = this.getResources().getDisplayMetrics().density;        Log.e("scale", "=" + scale);        int width = mIconMarker.getBitmap().getWidth(), height = mIconMarker.getBitmap().getHeight();//marker的获取宽高        bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444); //建立一个空的Bitmap        bitmap = scaleWithWH(bitmap, width * scale, height * scale);//缩放        //画笔进行添加文字(强烈推荐启舰的自定义控件三部曲http://blog.csdn.net/harvic880925/article/details/50995268)        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);//抗锯齿        paint.setDither(true); // 获取跟清晰的图像采样        paint.setFilterBitmap(true);// 过滤        paint.setColor(Color.RED);        paint.setTextSize(14 * scale);        Rect bounds = new Rect();        paint.getTextBounds(str, 0, str.length(), bounds);//获取文字的范围        //文字在mMarker中展示的位置        float paddingLeft = (bitmap.getWidth() - bounds.width()) / 2;//在中间        float paddingTop = (bitmap.getHeight() / scale);//在顶部        Canvas canvas = new Canvas(bitmap);        canvas.drawText(str, paddingLeft, paddingTop, paint);        //合并两个bitmap为一个        canvas.drawBitmap(mIconMarker.getBitmap(), paddingLeft, paddingTop, null);//marker的位置    }    private Bitmap scaleWithWH(Bitmap src, double w, double h) {        if (w == 0 || h == 0 || src == null) {            return src;        } else {            // 记录src的宽高            int width = src.getWidth();            int height = src.getHeight();            // 创建一个matrix容器            Matrix matrix = new Matrix();            // 计算缩放比例            float scaleWidth = (float) (w / width);            float scaleHeight = (float) (h / height);            // 开始缩放            matrix.postScale(scaleWidth, scaleHeight);            // 创建缩放后的图片            return Bitmap.createBitmap(src, 0, 0, width, height, matrix, true);        }    }    private void gc() {        if (bitmap != null) {            // 回收并且置为null            bitmap.recycle();            bitmap = null;        }        System.gc();    }}


参考博客:http://blog.csdn.net/dawanganban/article/details/51148070

0 0
原创粉丝点击