BaiduMap---百度地图官方Demo之自定义绘制功能(介绍自定义绘制点,线,多边形,园等几何图形和文字)

来源:互联网 发布:ipad买哪款好2017知乎 编辑:程序博客网 时间:2024/05/22 00:17
<?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/button1"            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:layout_marginBottom="10dip"            android:layout_marginLeft="10dip"            android:layout_marginRight="10dip"            android:layout_marginTop="10dip"            android:layout_weight="1.0"            android:text="清除(clear)" />        <Button            android:id="@+id/button2"            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:layout_marginBottom="10dip"            android:layout_marginLeft="10dip"            android:layout_marginRight="10dip"            android:layout_marginTop="10dip"            android:layout_weight="1.0"            android:text="重置(reset)" />    </LinearLayout>    <com.baidu.mapapi.map.MapView        android:id="@+id/bmapView"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:clickable="true" /></LinearLayout>




/** * 此demo用来展示如何在地图上用GraphicsOverlay添加点、线、多边形、圆 同时展示如何在地图上用TextOverlay添加文字 * 介绍自定义绘制点,线,多边形,圆等几何图形或文字 *  * Geometry:几何学 */public class GeometryDemo extends Activity {// 地图相关MapView mMapView;BaiduMap mBaiduMap;// UI相关Button resetBtn;Button clearBtn;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_geometry);//初始化地图mMapView = (MapView) findViewById(R.id.bmapView);mBaiduMap = mMapView.getMap();// UI初始化clearBtn = (Button) findViewById(R.id.button1);resetBtn = (Button) findViewById(R.id.button2);//清除OnClickListener clearListener = new OnClickListener() {public void onClick(View v) {clearClick();}};//重置OnClickListener restListener = new OnClickListener() {public void onClick(View v) {resetClick();}};clearBtn.setOnClickListener(clearListener);resetBtn.setOnClickListener(restListener);// 界面加载时添加绘制图层addCustomElementsDemo();}/** * 添加点、线、多边形、圆、文字 */public void addCustomElementsDemo() {// 添加折线LatLng p1 = new LatLng(39.97923, 116.357428);LatLng p2 = new LatLng(39.94923, 116.397428);LatLng p3 = new LatLng(39.97923, 116.437428);List<LatLng> points = new ArrayList<LatLng>();points.add(p1);points.add(p2);points.add(p3);/** *OverlayOptions:地图覆盖物选型基类 * *PolylineOptions:创建折线覆盖物选项类 * width(int width):设置折线线宽, 默认为 5, 单位:像素 *  color(int color):设置折线颜色 *  points(java.util.List<LatLng> points):设置折线坐标点列表 * */OverlayOptions ooPolyline = new PolylineOptions().width(10).color(0xAAFF0000).points(points);/** *addOverlay(OverlayOptions options): *向地图添加一个 Overlay * */mBaiduMap.addOverlay(ooPolyline);//添加弧线/** *ArcOptions:弧线构造选项,继承自 #OverlayOptions * color(int color):设置弧线的颜色 *  width(int width):设置弧线的线宽 *  points(LatLng start, LatLng middle, LatLng end):设置弧线的起点、中点、终点坐标 * */OverlayOptions ooArc = new ArcOptions().color(0xAA00FF00).width(4).points(p1, p2, p3);mBaiduMap.addOverlay(ooArc);// 添加圆LatLng llCircle = new LatLng(39.90923, 116.447428);/** *CircleOptions:创建圆的选项 * fillColor(int color):设置圆填充颜色 * center(LatLng center):设置圆心坐标 *   stroke(Stroke stroke):设置圆边框信息 *    *Stroke:边框类,可以给圆、多边形设置一个边框 * Stroke(int strokeWidth, int color): *    color:边框的颜色 *      strokeWidth:边框的宽度, 默认为 5, 单位:像素    * */OverlayOptions ooCircle = new CircleOptions().fillColor(0x000000FF).center(llCircle).stroke(new Stroke(5, 0xAA000000)).radius(1400);mBaiduMap.addOverlay(ooCircle);LatLng llDot = new LatLng(39.98923, 116.397428);OverlayOptions ooDot = new DotOptions().center(llDot).radius(6).color(0xFF0000FF);mBaiduMap.addOverlay(ooDot);// 添加多边形LatLng pt1 = new LatLng(39.93923, 116.357428);LatLng pt2 = new LatLng(39.91923, 116.327428);LatLng pt3 = new LatLng(39.89923, 116.347428);LatLng pt4 = new LatLng(39.89923, 116.367428);LatLng pt5 = new LatLng(39.91923, 116.387428);List<LatLng> pts = new ArrayList<LatLng>();pts.add(pt1);pts.add(pt2);pts.add(pt3);pts.add(pt4);pts.add(pt5);OverlayOptions ooPolygon = new PolygonOptions().points(pts).stroke(new Stroke(5, 0xAA00FF00)).fillColor(0xAAFFFF00);mBaiduMap.addOverlay(ooPolygon);// 添加文字LatLng llText = new LatLng(39.86923, 116.397428);OverlayOptions ooText = new TextOptions().bgColor(0xAAFFFF00).fontSize(24).fontColor(0xFFFF00FF).text("百度地图SDK").rotate(-30).position(llText);mBaiduMap.addOverlay(ooText);}public void resetClick() {// 添加绘制元素addCustomElementsDemo();}public void clearClick() {/** *clear():清空地图所有的 Overlay 覆盖物以及 InfoWindow * */mMapView.getMap().clear();}@Overrideprotected void onPause() {mMapView.onPause();super.onPause();}@Overrideprotected void onResume() {mMapView.onResume();super.onResume();}@Overrideprotected void onDestroy() {mMapView.onDestroy();super.onDestroy();}}


0 0
原创粉丝点击