百度地图创建InfoWindow自定义View显示
来源:互联网 发布:淘宝客程序app 编辑:程序博客网 时间:2024/04/18 18:03
最近学习使用百度地图,自己做了个可以简单搜索兴趣点,设置搜索范围,搜索关键字,定位功能的应用。在使用百度地图API时发现了问题。
我想要完成的需求是:
在地图上长按然后弹出自定义的View,并且能与用户进行交互操作。
但在百度地图开发指南中关于弹出覆盖物就简单的一段实例,如下:查看连接
从上面简单的代码看出,InfoWindow可以显示View,但上面的实例仅仅是使用代码动态的创建了一个简单的View,但能不能从xml初始化一个自定义复杂的View呢?
弹出窗覆盖物弹出窗覆盖物的实现方式如下,开发者可利用此接口,构建具有更强交互性的地图页面。//创建InfoWindow展示的view Button button = new Button(getApplicationContext()); button.setBackgroundResource(R.drawable.popup); //定义用于显示该InfoWindow的坐标点 LatLng pt = new LatLng(39.86923, 116.397428); //创建InfoWindow , 传入 view, 地理坐标, y 轴偏移量 InfoWindow mInfoWindow = new InfoWindow(button, pt, -47); //显示InfoWindow mBaiduMap.showInfoWindow(mInfoWindow);下图为点击Marker弹出InfoWindow的示例图,开发者只需将InfoWindow的显示方法写在Maker的点击事件处理中即可实现该效果。运行结果如下:
从上面简单的代码看出,InfoWindow可以显示View,但上面的实例仅仅是使用代码动态的创建了一个简单的View,但能不能从xml初始化一个自定义复杂的View呢?
在网上查阅资料发现各种答案,可能是度娘的原因,反正是没有找到有效的方法。有的解决了,但方法超复杂,不忍直视了。
自己就去查阅百度API,在InfoWindow的构造方法:
InfoWindow public InfoWindow(View view, LatLng position, int yOffset) 通过传入的 view 构造一个 InfoWindow, 此时只是利用该view生成一个Bitmap绘制在地图中。 参数: view - InfoWindow 展示的 view position - InfoWindow 显示的地理位置 yOffset - InfoWindow Y 轴偏移量 listener - InfoWindow 点击监听者 抛出: java.lang.IllegalArgumentException - view 和 position 不能为 null
发现了InfoWindow的构造方法中,第一参数要求的是View,那能不能传入一个自定义的View呢,说干就干,就自己创建一个View,并将其传入InfoWindow,核心代码如下:
//====================地图长按监听============================= @Override public void onMapLongClick(final LatLng latLng) { //将长按点设置为地图显示中心 MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newLatLng(latLng); map.animateMapStatus(mapStatusUpdate); map.clear();//清空地图上的标记 //长按点位置显示标记 markerOptions = new MarkerOptions(); markerOptions.position(latLng); BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.location1); markerOptions.icon(bitmapDescriptor); markerOptions.zIndex(9); Marker marker = (Marker) map.addOverlay(markerOptions); //从xml创建要显示的View,并设置相应的值 LayoutInflater inflater = LayoutInflater.from(getApplicationContext()); View view = inflater.inflate(R.layout.layout_map_item, null); TextView txtLatLng = (TextView)view.findViewById(R.id.text_item_latlng); final EditText background = (EditText) view.findViewById(R.id.ed_item_background); final EditText keyWord = (EditText) view.findViewById(R.id.ed_item_keyword); Button btnSearch = (Button) view.findViewById(R.id.btn_search); Button btnCancel = (Button) view.findViewById(R.id.btn_cancel); txtLatLng.setText("纬度:"+latLng.latitude+",经度:"+latLng.longitude); final LatLng lngFinal = latLng; //点击view上面的检索按钮调用方法 btnSearch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //Log.d("WorkMainActivity","搜索附近500米"); map.clear();//清空地图上的标记 String circumBackground = background.getText().toString(); if(null==circumBackground||"".equals(circumBackground)){ return; }else { String keyWordString = keyWord.getText().toString(); if(null==keyWordString||"".equals(keyWordString)){ return; }else { int circum = Integer.parseInt(circumBackground); PoiNearbySearchOption poiNearbySearchOption = new PoiNearbySearchOption(); poiNearbySearchOption.location(lngFinal); //以长按坐标点为中心,画指定半径的圆,并制定透明度为100,作为搜索范围 CircleOptions circleOptions = new CircleOptions(); circleOptions.center(lngFinal); circleOptions.radius(circum); circleOptions.fillColor(Color.argb(100,28,95,167)); map.addOverlay(circleOptions); poiNearbySearchOption.keyword(keyWordString); poiNearbySearchOption.radius(circum); poiSearch.searchNearby(poiNearbySearchOption); poiSearch.setOnGetPoiSearchResultListener(WorkMainActivity.this); } } } }); //点击取消按钮 btnCancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //Log.d("WorkMainActivity","取消搜索"); map.hideInfoWindow(); } }); InfoWindow infoWindow = new InfoWindow(view, latLng, -47); map.showInfoWindow(infoWindow); }
至于View的定义文件,即layout_map_item文件代码如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:background="#2B9685" android:layout_height="wrap_content"> <TextView android:layout_width="100dp" android:layout_height="wrap_content" android:text="搜索附近" android:textSize="20sp" android:layout_gravity="center_horizontal" android:gravity="center_horizontal" /> <TextView android:id="@+id/text_item_latlng" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:background="#37B158" /> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/ed_item_background" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#58C7F9" android:hint="范围(m)" /> <EditText android:id="@+id/ed_item_keyword" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#BCEAA7" android:hint="关键字" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/btn_search" android:layout_width="0dp" android:layout_height="wrap_content" android:background="#BABA66" android:textSize="20sp" android:text="检索" android:layout_weight="1" /> <Button android:id="@+id/btn_cancel" android:layout_width="0dp" android:layout_height="wrap_content" android:background="#BABAFF" android:textSize="20sp" android:layout_weight="1" android:text="取消" /> </LinearLayout></LinearLayout>
结果发现,确实可行,运行效果如下:(注:该程序编译版本API 19,需要手机,模拟器4.4.2以上的版本才能正常运行,如果版本过低,可以更改 AndroidManifest.xml中的<uses-sdk android:minSdkVersion="19"/>标签,改成相应版本即可)
(下面附上程序全部代码)
下载源代码
0 1
- 百度地图创建InfoWindow自定义View显示
- 百度地图创建InfoWindow自定义View显示
- 实现地图上显示多mark点,自定义infoWindow,跳转高德地图和百度地图
- 关于百度地图InfoWindow响应自定义布局点击事件
- 百度地图InfoWindow循环显示不同的信息窗口
- 入门Android开发--高德地图--自定义infowindow不显示
- 高德地图 amap 显示 marker和自定义的infoWindow
- android高德地图自定义infoWindow没有显示出来
- 百度地图InfoWindow事件绑定
- 安卓高德地图之自定义infowindow
- 百度地图显示自定义头像
- 关于百度地图infowindow报错the constructor InfoWindow undefined
- 百度地图的添加覆盖物marker和InfoWindow总结,自定义标签
- 百度地图 Android 设置 InfoWindow 不同缩放级别均能居中(自定义偏移量)
- 使用百度地图BMap.InfoWindow,分别显示不同的信息窗口
- 关于百度地图 BMap.InfoWindow 只显示最后一条信息解决方法
- 关于百度地图 BMap.InfoWindow 只显示最后一条信息解决方法
- (Android)百度地图API只能显示一个infowindow的完美解决方案
- The import android.support.v7 cannot be resolved 完美解决方案
- wifi耗电问题的分析的几个方向!
- Android布局大全
- Android 5.0 Launcher客制化定制之 主题包协议(MIUI主题、乐蛙主题兼容)
- sadlkasjldasjkljdakldjak卡刷的撒空间
- 百度地图创建InfoWindow自定义View显示
- Unity3D学习日志第一天
- Android Scroll详解(二):OverScroller实战
- Android开发:锁屏状态下弹出Activity
- 黄聪:pycharm开发python 在python Console中输出的程序有乱码,含有中文的输出
- jQuery.extend 函数详解
- Android MotionEvent详解
- html学习之
- 【iOS界面处理】使用storyboard实现页面跳转,简单的数据传递