zoukankan      html  css  js  c++  java
  • Android百度地图之自定义绘制点、线、圆、多边形实现

    我们可以在地图上绘制各种自定义的图形,包括点、折线、圆、多边形等等,尤其绘制点和折线非常实用,点可以用来标识所处的位置,折线可以用来描述走过的轨迹,结合前面GPS定位功能可以做出一些非常有意思的应用,下面应用百度Demo实现绘制的基本功能,代码如下:

    Activity:

    package com.home;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    import com.baidu.mapapi.map.Geometry;
    import com.baidu.mapapi.map.Graphic;
    import com.baidu.mapapi.map.GraphicsOverlay;
    import com.baidu.mapapi.map.MapView;
    import com.baidu.mapapi.map.Symbol;
    import com.baidu.mapapi.map.TextItem;
    import com.baidu.mapapi.map.TextOverlay;
    import com.baidu.platform.comapi.basestruct.GeoPoint;
    
    /**
     * 此demo用来展示如何在地图上用GraphicsOverlay添加点、线、多边形、圆 同时展示如何在地图上用TextOverlay添加文字
     * 
     */
    public class GeometryActivity extends Activity implements OnClickListener {
    
        // 地图相关
        private MapView mMapView = null;
    
        private Button resetBtn = null;
        private Button clearBtn = null;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_geometry);
            CharSequence titleLable = "自定义绘制功能";
            setTitle(titleLable);
    
            // 初始化地图
            mMapView = (MapView) findViewById(R.id.bmapView);
            mMapView.getController().setZoom(12.5f);
            mMapView.getController().enableClick(true);
    
            // UI初始化
            clearBtn = (Button) findViewById(R.id.btn_clear);
            resetBtn = (Button) findViewById(R.id.btn_reset);
            clearBtn.setOnClickListener(this);
            resetBtn.setOnClickListener(this);
            resetBtn.setEnabled(false);
    
            // 界面加载时添加绘制图层
            addCustomElementsDemo();
        }
    
        @Override
        public void onClick(View v) {
            if (v == clearBtn) {
                clearClick();
                clearBtn.setEnabled(false);
                resetBtn.setEnabled(true);
            }
            if (v == resetBtn) {
                resetClick();
                clearBtn.setEnabled(true);
                resetBtn.setEnabled(false);
            }
        }
    
        /**
         * 清除所有图层
         */
        public void clearClick() {
            mMapView.getOverlays().clear();
        }
    
        /**
         * 添加绘制元素
         */
        public void resetClick() {
            addCustomElementsDemo();
        }
    
        /**
         * 添加点、线、多边形、圆、文字
         */
        public void addCustomElementsDemo() {
            GraphicsOverlay graphicsOverlay = new GraphicsOverlay(mMapView);
            mMapView.getOverlays().add(graphicsOverlay);
            // 添加点
            graphicsOverlay.setData(drawPoint());
            // 添加折线
            graphicsOverlay.setData(drawLine());
            // 添加多边形
            graphicsOverlay.setData(drawPolygon());
            // 添加圆
            graphicsOverlay.setData(drawCircle());
            // 绘制文字
            TextOverlay textOverlay = new TextOverlay(mMapView);
            mMapView.getOverlays().add(textOverlay);
            textOverlay.addText(drawText());
            // 执行地图刷新使生效
            mMapView.refresh();
        }
    
        /**
         * 绘制折线,该折线状态随地图状态变化
         * 
         * @return 折线对象
         */
        public Graphic drawLine() {
            double mLat = 39.97923;
            double mLon = 116.357428;
    
            int lat = (int) (mLat * 1E6);
            int lon = (int) (mLon * 1E6);
            GeoPoint pt1 = new GeoPoint(lat, lon);
    
            mLat = 39.94923;
            mLon = 116.397428;
            lat = (int) (mLat * 1E6);
            lon = (int) (mLon * 1E6);
            GeoPoint pt2 = new GeoPoint(lat, lon);
            mLat = 39.97923;
            mLon = 116.437428;
            lat = (int) (mLat * 1E6);
            lon = (int) (mLon * 1E6);
            GeoPoint pt3 = new GeoPoint(lat, lon);
    
            // 构建线
            Geometry lineGeometry = new Geometry();
            // 设定折线点坐标
            GeoPoint[] linePoints = new GeoPoint[3];
            linePoints[0] = pt1;
            linePoints[1] = pt2;
            linePoints[2] = pt3;
            lineGeometry.setPolyLine(linePoints);
            // 设定样式
            Symbol lineSymbol = new Symbol();
            Symbol.Color lineColor = lineSymbol.new Color();
            lineColor.red = 255;
            lineColor.green = 0;
            lineColor.blue = 0;
            lineColor.alpha = 255;
            lineSymbol.setLineSymbol(lineColor, 10);
            // 生成Graphic对象
            Graphic lineGraphic = new Graphic(lineGeometry, lineSymbol);
            return lineGraphic;
        }
    
        /**
         * 绘制多边形,该多边形随地图状态变化
         * 
         * @return 多边形对象
         */
        public Graphic drawPolygon() {
            double mLat = 39.93923;
            double mLon = 116.357428;
            int lat = (int) (mLat * 1E6);
            int lon = (int) (mLon * 1E6);
            GeoPoint pt1 = new GeoPoint(lat, lon);
            mLat = 39.91923;
            mLon = 116.327428;
            lat = (int) (mLat * 1E6);
            lon = (int) (mLon * 1E6);
            GeoPoint pt2 = new GeoPoint(lat, lon);
            mLat = 39.89923;
            mLon = 116.347428;
            lat = (int) (mLat * 1E6);
            lon = (int) (mLon * 1E6);
            GeoPoint pt3 = new GeoPoint(lat, lon);
            mLat = 39.89923;
            mLon = 116.367428;
            lat = (int) (mLat * 1E6);
            lon = (int) (mLon * 1E6);
            GeoPoint pt4 = new GeoPoint(lat, lon);
            mLat = 39.91923;
            mLon = 116.387428;
            lat = (int) (mLat * 1E6);
            lon = (int) (mLon * 1E6);
            GeoPoint pt5 = new GeoPoint(lat, lon);
    
            // 构建多边形
            Geometry polygonGeometry = new Geometry();
            // 设置多边形坐标
            GeoPoint[] polygonPoints = new GeoPoint[5];
            polygonPoints[0] = pt1;
            polygonPoints[1] = pt2;
            polygonPoints[2] = pt3;
            polygonPoints[3] = pt4;
            polygonPoints[4] = pt5;
            polygonGeometry.setPolygon(polygonPoints);
            // 设置多边形样式
            Symbol polygonSymbol = new Symbol();
            Symbol.Color polygonColor = polygonSymbol.new Color();
            polygonColor.red = 0;
            polygonColor.green = 0;
            polygonColor.blue = 255;
            polygonColor.alpha = 126;
            polygonSymbol.setSurface(polygonColor, 1, 5);
            // 生成Graphic对象
            Graphic polygonGraphic = new Graphic(polygonGeometry, polygonSymbol);
            return polygonGraphic;
        }
    
        /**
         * 绘制单点,该点状态不随地图状态变化而变化
         * 
         * @return 点对象
         */
        public Graphic drawPoint() {
            double mLat = 39.98923;
            double mLon = 116.397428;
            int lat = (int) (mLat * 1E6);
            int lon = (int) (mLon * 1E6);
            GeoPoint pt1 = new GeoPoint(lat, lon);
    
            // 构建点
            Geometry pointGeometry = new Geometry();
            // 设置坐标
            pointGeometry.setPoint(pt1, 10);
            // 设定样式
            Symbol pointSymbol = new Symbol();
            Symbol.Color pointColor = pointSymbol.new Color();
            pointColor.red = 0;
            pointColor.green = 126;
            pointColor.blue = 255;
            pointColor.alpha = 255;
            pointSymbol.setPointSymbol(pointColor);
            // 生成Graphic对象
            Graphic pointGraphic = new Graphic(pointGeometry, pointSymbol);
            return pointGraphic;
        }
    
        /**
         * 绘制圆,该圆随地图状态变化
         * 
         * @return 圆对象
         */
        public Graphic drawCircle() {
            double mLat = 39.90923;
            double mLon = 116.447428;
            int lat = (int) (mLat * 1E6);
            int lon = (int) (mLon * 1E6);
            GeoPoint pt1 = new GeoPoint(lat, lon);
    
            // 构建圆
            Geometry circleGeometry = new Geometry();
    
            // 设置圆中心点坐标和半径
            circleGeometry.setCircle(pt1, 2500);
            // 设置样式
            Symbol circleSymbol = new Symbol();
            Symbol.Color circleColor = circleSymbol.new Color();
            circleColor.red = 0;
            circleColor.green = 255;
            circleColor.blue = 0;
            circleColor.alpha = 126;
            circleSymbol.setSurface(circleColor, 1, 3);
            // 生成Graphic对象
            Graphic circleGraphic = new Graphic(circleGeometry, circleSymbol);
            return circleGraphic;
        }
    
        /**
         * 绘制文字,该文字随地图变化有透视效果
         * 
         * @return 文字对象
         */
        public TextItem drawText() {
            double mLat = 39.86923;
            double mLon = 116.397428;
            int lat = (int) (mLat * 1E6);
            int lon = (int) (mLon * 1E6);
            // 构建文字
            TextItem item = new TextItem();
            // 设置文字位置
            item.pt = new GeoPoint(lat, lon);
            // 设置文件内容
            item.text = "百度地图SDK";
            // 设文字大小
            item.fontSize = 40;
            Symbol symbol = new Symbol();
            Symbol.Color bgColor = symbol.new Color();
            // 设置文字背景色
            bgColor.red = 0;
            bgColor.blue = 0;
            bgColor.green = 255;
            bgColor.alpha = 50;
    
            Symbol.Color fontColor = symbol.new Color();
            // 设置文字着色
            fontColor.alpha = 255;
            fontColor.red = 0;
            fontColor.green = 0;
            fontColor.blue = 255;
            // 设置对齐方式
            item.align = TextItem.ALIGN_CENTER;
            // 设置文字颜色和背景颜色
            item.fontColor = fontColor;
            item.bgColor = bgColor;
            return item;
        }
    
        @Override
        protected void onPause() {
            mMapView.onPause();
            super.onPause();
        }
    
        @Override
        protected void onResume() {
            mMapView.onResume();
            super.onResume();
        }
    
        @Override
        protected void onDestroy() {
            mMapView.destroy();
            super.onDestroy();
        }
    
        @Override
        protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            mMapView.onSaveInstanceState(outState);
    
        }
    
        @Override
        protected void onRestoreInstanceState(Bundle savedInstanceState) {
            super.onRestoreInstanceState(savedInstanceState);
            mMapView.onRestoreInstanceState(savedInstanceState);
        }
    }

    布局XMl:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    
            <Button
                android:id="@+id/btn_clear"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:layout_weight="1.0"
                android:background="@drawable/button_style"
                android:text="清除(clear)" />
    
            <Button
                android:id="@+id/btn_reset"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:layout_weight="1.0"
                android:background="@drawable/button_style"
                android:text="重置(reset)" />
        </LinearLayout>
    
        <com.baidu.mapapi.map.MapView
            android:id="@+id/bmapView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clickable="true" />
    
    </LinearLayout>

    Manifest配置跟前面一样。

    附上图片效果:

    d6.png

    来自:http://blog.csdn.net/u010142437/article/details/11422213

  • 相关阅读:
    hadoop再次集群搭建(3)-如何选择相应的hadoop版本
    48. Rotate Image
    352. Data Stream as Disjoint Interval
    163. Missing Ranges
    228. Summary Ranges
    147. Insertion Sort List
    324. Wiggle Sort II
    215. Kth Largest Element in an Array
    快速排序
    280. Wiggle Sort
  • 原文地址:https://www.cnblogs.com/a354823200/p/3930651.html
Copyright © 2011-2022 走看看