百度地图多个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
- 百度地图多个InfoWindow同时展示
- 百度地图4.0多个经纬度展示在地图上
- 百度地图InfoWindow事件绑定
- BaiduMap---百度地图官方Demo之多地图展示(在一个Activity中创建多个地图展示)
- 关于百度地图infowindow报错the constructor InfoWindow undefined
- 实现地图上显示多mark点,自定义infoWindow,跳转高德地图和百度地图
- 百度地图-同时加载多张地图
- 百度地图infoWindow圆角处理
- 百度地图3.2.0InfoWindow使用总结
- 百度地图创建InfoWindow自定义View显示
- 百度地图创建InfoWindow自定义View显示
- 百度地图最新版本sdk使用系列(六)-搜索周边美食,加油站等,并使用infoWindow展示
- 百度地图中实现历史轨迹显示为BMap.InfoWindow添加多个不同时间点但相同经纬度点的信息
- 百度地图页面展示
- 百度地图api 怎么禁止百度自己的InfoWindow
- 百度地图------百度infowindow重影问题记录我的
- 百度地图InfoWindow循环显示不同的信息窗口
- 百度地图覆盖物Marker与infoWindow的使用
- 在java使用xssProject
- 剑指offer--变态跳台阶
- 信号------信号的基本概念
- 提取不同颜色细胞
- netty中拆包粘包问题
- 百度地图多个InfoWindow同时展示
- 欢迎使用CSDN-markdown编辑器
- 阻塞队列BlockingQueue(JDK8)
- hadoop学习(8)—— 常见配置
- 前端小白训练营DAY1
- nginx学习9——修改worker的进程名,修改master的名称,cpu策略,最大并发策略
- 全志A33 fastboot模式探秘
- LeetCode01 Two_Sum
- Python 继承之 self的解释