您的位置:首页 > 大数据 > 人工智能

BaiduMap---百度地图官方Demo之覆盖物功能(介绍添加覆盖物并响应点击功能和弹出pop功能)

2015-07-02 11:26 721 查看
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/clear"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginBottom="2dip"
            android:layout_marginLeft="2dip"
            android:layout_marginRight="2dip"
            android:layout_marginTop="2dip"
            android:layout_weight="1"
            android:onClick="clearOverlay"
            android:padding="10dip"
            android:text="清除(clear)" />

        <Button
            android:id="@+id/resert"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginBottom="2dip"
            android:layout_marginLeft="2dip"
            android:layout_marginRight="2dip"
            android:layout_marginTop="2dip"
            android:layout_weight="1"
            android:onClick="resetOverlay"
            android:text="重置(reset)" />
    </LinearLayout>

    <com.baidu.mapapi.map.MapView
        android:id="@+id/bmapView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>




/**
 * 演示覆盖物的用法
 * 
 * 介绍添加覆盖物并响应点击功能和弹出pop功能
 */
public class OverlayDemo extends Activity {

	/**
	 * MapView 是地图主控件
	 */
	private MapView mMapView;
	private BaiduMap mBaiduMap;
	
	/**
	 * 定义地图 Marker 覆盖物
	 * */
	private Marker mMarkerA;
	private Marker mMarkerB;
	private Marker mMarkerC;
	private Marker mMarkerD;
	
	/**
	 * 在地图中显示一个信息窗口,可以设置一个View作为该窗口的内容,
	 * 也可以设置一个 BitmapDescriptor 作为该窗口的内容。
	 * */
	private InfoWindow mInfoWindow;

	// 初始化全局 bitmap 信息,不用时及时 recycle
	BitmapDescriptor bdA = BitmapDescriptorFactory
			.fromResource(R.drawable.icon_marka);
	BitmapDescriptor bdB = BitmapDescriptorFactory
			.fromResource(R.drawable.icon_markb);
	BitmapDescriptor bdC = BitmapDescriptorFactory
			.fromResource(R.drawable.icon_markc);
	BitmapDescriptor bdD = BitmapDescriptorFactory
			.fromResource(R.drawable.icon_markd);
	BitmapDescriptor bd = BitmapDescriptorFactory
			.fromResource(R.drawable.icon_gcoding);
	BitmapDescriptor bdGround = BitmapDescriptorFactory
			.fromResource(R.drawable.ground_overlay);

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_overlay);
		mMapView = (MapView) findViewById(R.id.bmapView);
		mBaiduMap = mMapView.getMap();
		
		/**
		 * MapStatusUpdate:描述地图状态将要发生的变化
		 * 
		 * MapStatusUpdateFactory:生成地图状态将要发生的变化
		 * public static MapStatusUpdate zoomTo(float zoom)
		 * 设置地图缩放级别
		 * 参数:zoom - 地图缩放级别
		 * 返回:返回构造的 MapStatusUpdate 对象
		 * */
		MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(14.0f);
		mBaiduMap.setMapStatus(msu);
		
		initOverlay();
		
		mBaiduMap.setOnMarkerClickListener(new OnMarkerClickListener() {
			public boolean onMarkerClick(final Marker marker) {
				Button button = new Button(getApplicationContext());
				button.setBackgroundResource(R.drawable.popup);
				
				/**
				 * 信息窗口点击事件监听接口
				 * */
				OnInfoWindowClickListener listener = null;
				//点击A和D时更改位置
				if (marker == mMarkerA || marker == mMarkerD) {
					button.setText("更改位置");
					listener = new OnInfoWindowClickListener() {
						//信息窗口点击事件处理函数
						public void onInfoWindowClick() {
							/**
							 * public LatLng getPosition()
							 * 获取 Marker 覆盖物的位置坐标
							 * 返回:Marker 覆盖物的位置坐标
							 * */
							LatLng ll = marker.getPosition();
							LatLng llNew = new LatLng(ll.latitude + 0.005,ll.longitude + 0.005);
							
							/**
							 * public void setPosition(LatLng position)
							 * 设置 Marker 覆盖物的位置坐标
							 * 参数:position - Marker 覆盖物的位置坐标
							 * */
							marker.setPosition(llNew);
							//隐藏当前 InfoWindow
							mBaiduMap.hideInfoWindow();
						}
					};
					LatLng ll = marker.getPosition();
					
					/**
					 * public static BitmapDescriptor fromView(View view)
					 * 根据一个 View 创建 Bitmap 描述信息, 当 view 为 null 时返回 null
					 * 参数:view - 
					 * 返回:Bitmap 描述信息, 当 view 为 null 时返回 null
					 * 
					 * public InfoWindow(BitmapDescriptor bd,
          								LatLng position,
          								int yOffset,
          					InfoWindow.OnInfoWindowClickListener listener)
          					通过传入的 bitmap descriptor 构造一个 InfoWindow。

						参数:
						    bd - InfoWindow 展示的bitmap
						    position - InfoWindow 显示的地理位置
						    yOffset - InfoWindow Y 轴偏移量
						    listener - InfoWindow 点击监听者
					 * */
					mInfoWindow = new InfoWindow(BitmapDescriptorFactory.fromView(button), ll, -47, listener);
					/**
					 * public void showInfoWindow(InfoWindow infoWindow)
					 * 显示 InfoWindow
					 * 参数:infoWindow - 要显示的 InfoWindow 对象
					 * */
					mBaiduMap.showInfoWindow(mInfoWindow);
					
					//点击B时更改位置
				} else if (marker == mMarkerB) {
					button.setText("更改图标");
					button.setOnClickListener(new OnClickListener() {
						public void onClick(View v) {
							marker.setIcon(bd);
							mBaiduMap.hideInfoWindow();
						}
					});
					LatLng ll = marker.getPosition();
					mInfoWindow = new InfoWindow(button, ll, -47);
					mBaiduMap.showInfoWindow(mInfoWindow);
					//点击C时删除
				} else if (marker == mMarkerC) {
					button.setText("删除");
					button.setOnClickListener(new OnClickListener() {
						public void onClick(View v) {
							/**
							 * 删除该覆盖物
							 * */
							marker.remove();
							mBaiduMap.hideInfoWindow();
						}
					});
					LatLng ll = marker.getPosition();
					mInfoWindow = new InfoWindow(button, ll, -47);
					mBaiduMap.showInfoWindow(mInfoWindow);
				}
				return true;
			}
		});
	}

	public void initOverlay() {
		// add marker overlay
		LatLng llA = new LatLng(39.963175, 116.400244);
		LatLng llB = new LatLng(39.942821, 116.369199);
		LatLng llC = new LatLng(39.939723, 116.425541);
		LatLng llD = new LatLng(39.906965, 116.401394);
		
		/**
		 * public final class MarkerOptions extends OverlayOptions
		 * 
		 * public MarkerOptions position(LatLng position)
		 * 设置 marker 覆盖物的位置坐标
		 * 参数:position - marker 覆盖物的位置坐标
		 * 返回:该 Marker 选项对象
		 * 
		 * public MarkerOptions icon(BitmapDescriptor icon)
		 * 设置 Marker 覆盖物的图标,相同图案的 icon 的 marker 最好使用同一个 BitmapDescriptor 对象以节省内存空间。
		 * 参数:icon - Marker 覆盖物的图标
		 * 返回:该 Marker 选项对象
		 * 
		 * public MarkerOptions zIndex(int zIndex)
		 * 设置 marker 覆盖物的 zIndex
		 * 参数:zIndex - marker 覆盖物的 zIndex
		 * 返回: 该 Marker 选项对象
		 * 
		 * public MarkerOptions draggable(boolean draggable)
		 * 设置 marker 是否允许拖拽,默认不可拖拽
		 * 参数:draggable - marker 是否允许拖拽
		 * 返回:该 Marker 选项对象
		 * 
		 * public MarkerOptions perspective(boolean perspective)
		 * 设置是否开启 marker 覆盖物近大远小效果,默认开启
		 * 参数:perspective - 是否开启 marker 覆盖物近大远小效果
		 * 返回:该 Marker选项对象
		 * 
		 * public MarkerOptions anchor(float anchorX,float anchorY)
		 * 设置 marker 覆盖物的锚点比例,默认(0.5f, 1.0f)水平居中,垂直下对齐
		 * 参数:
		 * anchorX - [0.0f , 1.0f], 否则不生效
		 * anchorY - [0.0f , 1.0f], 否则不生效
		 * 返回:该 Marker 选项对象
		 * 
		 * public MarkerOptions rotate(float rotate)
		 * 设置 marker 覆盖物旋转角度,逆时针
		 * 参数:rotate - marker 覆盖物旋转角度,逆时针
		 * 返回: 该 Marker 选项对象
		 * */
		OverlayOptions ooA = new MarkerOptions().position(llA).icon(bdA).zIndex(9).draggable(true);
		/**
		 * public final Overlay addOverlay(OverlayOptions options)
		 * 向地图添加一个 Overlay
		 * 参数:options - 将要添加的 overlay 选项
		 * 返回:被添加的 overlay 对象
		 * */
		mMarkerA = (Marker) (mBaiduMap.addOverlay(ooA));
		
		OverlayOptions ooB = new MarkerOptions().position(llB).icon(bdB).zIndex(5);
		mMarkerB = (Marker) (mBaiduMap.addOverlay(ooB));
		
		OverlayOptions ooC = new MarkerOptions().position(llC).icon(bdC).perspective(false)
				                                .anchor(0.5f, 0.5f).rotate(30).zIndex(7);
		mMarkerC = (Marker) (mBaiduMap.addOverlay(ooC));
		
		ArrayList<BitmapDescriptor> giflist = new ArrayList<BitmapDescriptor>();
		giflist.add(bdA);
		giflist.add(bdB);
		giflist.add(bdC);
		OverlayOptions ooD = new MarkerOptions().position(llD).icons(giflist)
				                                .zIndex(0).period(10);	
		mMarkerD = (Marker) (mBaiduMap.addOverlay(ooD));

		// add ground overlay
		LatLng southwest = new LatLng(39.92235, 116.380338);
		LatLng northeast = new LatLng(39.947246, 116.414977);
		/**
		 *LatLngBounds.Builder:地理范围构造器
		 *
		 *public LatLngBounds.Builder include(LatLng point)
		 *让该地理范围包含一个地理位置坐标
		 *参数:point - 地理位置坐标
		 *返回:该构造器对象
		 *
		 *public LatLngBounds build()
		 *创建地理范围对象
		 *返回:创建出的地理范围对象
		 * */
		LatLngBounds bounds = new LatLngBounds.Builder()
							.include(northeast)
							.include(southwest)
							.build();
		
		/**
		 * GroundOverlayOptions:构造 ground 覆盖物的选项类
		 * 
		 * public GroundOverlayOptions positionFromBounds(LatLngBounds bounds)
		 * 设置 ground 覆盖物的位置信息方式二,设置西南与东北坐标范围
		 * 参数:bounds - 西南与东北坐标范围
		 * 返回:该 ground 覆盖物选项对象
		 * 
		 * public GroundOverlayOptions image(BitmapDescriptor image)
		 * 设置 Ground 覆盖物的图片信息参数:image - Ground 覆盖物的图片信息
		 * 返回:该 ground 覆盖物选项对象
		 * 
		 *public GroundOverlayOptions transparency(float transparency)
		 *设置 ground 覆盖物透明度
		 *参数:transparency - 范围:[0.0f , 1.0f], 否则不生效
		 *返回:该 ground 覆盖物选项对象
		 * */
		OverlayOptions ooGround = new GroundOverlayOptions()
								  .positionFromBounds(bounds)
								  .image(bdGround)
								  .transparency(0.8f);
		mBaiduMap.addOverlay(ooGround);
		
		/**
		 * public static MapStatusUpdate newLatLng(LatLng latLng)
		 * 设置地图新中心点
		 * 参数:
		 * latLng - 地图新中心点
		 * 返回:返回构造的 MapStatusUpdate 对象,如果 latLng 为 null 则返回空。
		 * 
		 * public LatLng getCenter()
		 * 获取该地理范围的中心地理坐标
		 * 返回:该地理范围的中心地理坐标
		 * */
		MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(bounds.getCenter());
		mBaiduMap.setMapStatus(u);

		mBaiduMap.setOnMarkerDragListener(new OnMarkerDragListener() {
			public void onMarkerDrag(Marker marker) {
				
			}

			public void onMarkerDragEnd(Marker marker) {
				/**
				 * public LatLng getPosition()
				 * 获取 Marker 覆盖物的位置坐标
				 * 返回:Marker 覆盖物的位置坐标
				 * */
				Toast.makeText(
						OverlayDemo.this,
						"拖拽结束,新位置:" + marker.getPosition().latitude + ", "+ marker.getPosition().longitude,
						Toast.LENGTH_LONG).show();
			}

			public void onMarkerDragStart(Marker marker) {
			}
		});
	}

	/**
	 * 清除按钮点击事件
	 * 
	 * 清除所有Overlay
	 * 
	 * @param view
	 */
	public void clearOverlay(View view) {
		mBaiduMap.clear();
	}

	/**
	 * 重置按钮点击事件
	 * 
	 * 重新添加Overlay
	 * 
	 * @param view
	 */
	public void resetOverlay(View view) {
		clearOverlay(null);
		initOverlay();
	}

	@Override
	protected void onPause() {
		// MapView的生命周期与Activity同步,当activity挂起时需调用MapView.onPause()
		mMapView.onPause();
		super.onPause();
	}

	@Override
	protected void onResume() {
		// MapView的生命周期与Activity同步,当activity恢复时需调用MapView.onResume()
		mMapView.onResume();
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		// MapView的生命周期与Activity同步,当activity销毁时需调用MapView.destroy()
		mMapView.onDestroy();
		super.onDestroy();
		// 回收 bitmap 资源
		bdA.recycle();
		bdB.recycle();
		bdC.recycle();
		bdD.recycle();
		bd.recycle();
		bdGround.recycle();
	}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: