百度地图创建InfoWindow自定义View显示
来源:互联网 发布:什么软件可以ps图片 编辑:程序博客网 时间:2024/03/28 18:59
- 弹出窗覆盖物
- 弹出窗覆盖物的实现方式如下,开发者可利用此接口,构建具有更强交互性的地图页面。
- //创建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"/>标签,改成相应版本即可)
(下面附上程序全部代码)
下载源代码
// 截图,在SnapshotReadyCallback中保存图片到 sd 卡
mBaiduMap.snapshot(new SnapshotReadyCallback() {
public void onSnapshotReady(Bitmap snapshot) {
File file = new File("/mnt/sdcard/test.png");
FileOutputStream out;
try {
out = new FileOutputStream(file);
if (snapshot.compress(
Bitmap.CompressFormat.PNG, 100, out)) {
out.flush();
out.close();
}
Toast.makeText(MapControlDemo.this,
"屏幕截图成功,图片存在: " + file.toString(),
Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
- 百度地图创建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的完美解决方案
- 求职面试自我介绍技巧
- 修改swap交换分区的大小
- 杂想
- Android 上传中文到服务器时的乱码问题
- 面试时怎样做精彩的自我介绍
- 百度地图创建InfoWindow自定义View显示
- 浅谈求职面试礼仪
- 似然函数
- 服务器运维。。
- 未来十年的发展格局
- [Erlang]整数与浮点数转换、进制转换
- 面试中自我介绍技巧
- HDU ACM 1099 Lottery
- App适配iPhone 6/ Plus和iOS 8:10条小秘诀