百度地图多个InfoWindow同时展示
2017-04-16 13:58
211 查看
由于百度的InfoWindow只能展示一个,marker却不受限制,因此换了一个思路,在网上搜集了许多资料,最终实验成功。
通过创建一个自定义的bitmap然后以marker的形式添加到地图上。
在xml中,只有一个地图控件,因为手边没有真机,就用模拟器进行测试的,地图是用的是TextureMapView使用默认的MapVIew会报错。
activity内容如下图:
参考博客:http://blog.csdn.net/dawanganban/article/details/51148070
通过创建一个自定义的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
相关文章推荐
- 百度地图最新版本sdk使用系列(六)-搜索周边美食,加油站等,并使用infoWindow展示
- arcgis 中清除图层的时候,同时在界面上清除 激活的infowindow
- 百度地图同时显示多个路书
- 百度地图 省市区县 信息展示
- BaiduMap---百度地图官方Demo之定位图层展示(介绍定位图层的基本用法)
- 百度地图页面上展示
- Android下百度地图开发(一):基础地图展示
- Android 百度地图 SDK v3.0.0 (三) 添加覆盖物Marker与InfoWindow的使用
- 百度地图的展示
- Android 百度地图 SDK v3.0.0 (三) 添加覆盖物Marker与InfoWindow的使用
- [习题]FileUpload 批次上传 / 多档案同时上传,上传成功后展示图片 (VB语法)
- Android 百度地图 SDK v3.0.0 (三) 添加覆盖物Marker与InfoWindow的使用
- 『实践』百度地图同时显示多个路书
- HTML5获取地理位置及百度地图展示实例【转】
- Android 百度地图 SDK v3.0.0 (三) 添加覆盖物Marker与InfoWindow的使用
- IOS 百度地图使用问题(动画同时执行)
- android笔记之百度地图详情页展示
- 百度地图的手动描边展示功能
- HTML5获取当前地理位置并在百度地图上展示的实例
- android百度地图 添加覆盖物Marker与InfoWindow的使用