Android百度地图之自定义绘制功能

来源:互联网 发布:外贸进销存软件 编辑:程序博客网 时间:2024/05/22 15:23

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

Activity:

[java] view plaincopy
  1. package com.home;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.Button;  
  8.   
  9. import com.baidu.mapapi.map.Geometry;  
  10. import com.baidu.mapapi.map.Graphic;  
  11. import com.baidu.mapapi.map.GraphicsOverlay;  
  12. import com.baidu.mapapi.map.MapView;  
  13. import com.baidu.mapapi.map.Symbol;  
  14. import com.baidu.mapapi.map.TextItem;  
  15. import com.baidu.mapapi.map.TextOverlay;  
  16. import com.baidu.platform.comapi.basestruct.GeoPoint;  
  17.   
  18. /** 
  19.  * 此demo用来展示如何在地图上用GraphicsOverlay添加点、线、多边形、圆 同时展示如何在地图上用TextOverlay添加文字 
  20.  *  
  21.  */  
  22. public class GeometryActivity extends Activity implements OnClickListener {  
  23.   
  24.     // 地图相关  
  25.     private MapView mMapView = null;  
  26.   
  27.     private Button resetBtn = null;  
  28.     private Button clearBtn = null;  
  29.   
  30.     @Override  
  31.     public void onCreate(Bundle savedInstanceState) {  
  32.         super.onCreate(savedInstanceState);  
  33.         setContentView(R.layout.activity_geometry);  
  34.         CharSequence titleLable = "自定义绘制功能";  
  35.         setTitle(titleLable);  
  36.   
  37.         // 初始化地图  
  38.         mMapView = (MapView) findViewById(R.id.bmapView);  
  39.         mMapView.getController().setZoom(12.5f);  
  40.         mMapView.getController().enableClick(true);  
  41.   
  42.         // UI初始化  
  43.         clearBtn = (Button) findViewById(R.id.btn_clear);  
  44.         resetBtn = (Button) findViewById(R.id.btn_reset);  
  45.         clearBtn.setOnClickListener(this);  
  46.         resetBtn.setOnClickListener(this);  
  47.         resetBtn.setEnabled(false);  
  48.   
  49.         // 界面加载时添加绘制图层  
  50.         addCustomElementsDemo();  
  51.     }  
  52.   
  53.     @Override  
  54.     public void onClick(View v) {  
  55.         if (v == clearBtn) {  
  56.             clearClick();  
  57.             clearBtn.setEnabled(false);  
  58.             resetBtn.setEnabled(true);  
  59.         }  
  60.         if (v == resetBtn) {  
  61.             resetClick();  
  62.             clearBtn.setEnabled(true);  
  63.             resetBtn.setEnabled(false);  
  64.         }  
  65.     }  
  66.   
  67.     /** 
  68.      * 清除所有图层 
  69.      */  
  70.     public void clearClick() {  
  71.         mMapView.getOverlays().clear();  
  72.     }  
  73.   
  74.     /** 
  75.      * 添加绘制元素 
  76.      */  
  77.     public void resetClick() {  
  78.         addCustomElementsDemo();  
  79.     }  
  80.   
  81.     /** 
  82.      * 添加点、线、多边形、圆、文字 
  83.      */  
  84.     public void addCustomElementsDemo() {  
  85.         GraphicsOverlay graphicsOverlay = new GraphicsOverlay(mMapView);  
  86.         mMapView.getOverlays().add(graphicsOverlay);  
  87.         // 添加点  
  88.         graphicsOverlay.setData(drawPoint());  
  89.         // 添加折线  
  90.         graphicsOverlay.setData(drawLine());  
  91.         // 添加多边形  
  92.         graphicsOverlay.setData(drawPolygon());  
  93.         // 添加圆  
  94.         graphicsOverlay.setData(drawCircle());  
  95.         // 绘制文字  
  96.         TextOverlay textOverlay = new TextOverlay(mMapView);  
  97.         mMapView.getOverlays().add(textOverlay);  
  98.         textOverlay.addText(drawText());  
  99.         // 执行地图刷新使生效  
  100.         mMapView.refresh();  
  101.     }  
  102.   
  103.     /** 
  104.      * 绘制折线,该折线状态随地图状态变化 
  105.      *  
  106.      * @return 折线对象 
  107.      */  
  108.     public Graphic drawLine() {  
  109.         double mLat = 39.97923;  
  110.         double mLon = 116.357428;  
  111.   
  112.         int lat = (int) (mLat * 1E6);  
  113.         int lon = (int) (mLon * 1E6);  
  114.         GeoPoint pt1 = new GeoPoint(lat, lon);  
  115.   
  116.         mLat = 39.94923;  
  117.         mLon = 116.397428;  
  118.         lat = (int) (mLat * 1E6);  
  119.         lon = (int) (mLon * 1E6);  
  120.         GeoPoint pt2 = new GeoPoint(lat, lon);  
  121.         mLat = 39.97923;  
  122.         mLon = 116.437428;  
  123.         lat = (int) (mLat * 1E6);  
  124.         lon = (int) (mLon * 1E6);  
  125.         GeoPoint pt3 = new GeoPoint(lat, lon);  
  126.   
  127.         // 构建线  
  128.         Geometry lineGeometry = new Geometry();  
  129.         // 设定折线点坐标  
  130.         GeoPoint[] linePoints = new GeoPoint[3];  
  131.         linePoints[0] = pt1;  
  132.         linePoints[1] = pt2;  
  133.         linePoints[2] = pt3;  
  134.         lineGeometry.setPolyLine(linePoints);  
  135.         // 设定样式  
  136.         Symbol lineSymbol = new Symbol();  
  137.         Symbol.Color lineColor = lineSymbol.new Color();  
  138.         lineColor.red = 255;  
  139.         lineColor.green = 0;  
  140.         lineColor.blue = 0;  
  141.         lineColor.alpha = 255;  
  142.         lineSymbol.setLineSymbol(lineColor, 10);  
  143.         // 生成Graphic对象  
  144.         Graphic lineGraphic = new Graphic(lineGeometry, lineSymbol);  
  145.         return lineGraphic;  
  146.     }  
  147.   
  148.     /** 
  149.      * 绘制多边形,该多边形随地图状态变化 
  150.      *  
  151.      * @return 多边形对象 
  152.      */  
  153.     public Graphic drawPolygon() {  
  154.         double mLat = 39.93923;  
  155.         double mLon = 116.357428;  
  156.         int lat = (int) (mLat * 1E6);  
  157.         int lon = (int) (mLon * 1E6);  
  158.         GeoPoint pt1 = new GeoPoint(lat, lon);  
  159.         mLat = 39.91923;  
  160.         mLon = 116.327428;  
  161.         lat = (int) (mLat * 1E6);  
  162.         lon = (int) (mLon * 1E6);  
  163.         GeoPoint pt2 = new GeoPoint(lat, lon);  
  164.         mLat = 39.89923;  
  165.         mLon = 116.347428;  
  166.         lat = (int) (mLat * 1E6);  
  167.         lon = (int) (mLon * 1E6);  
  168.         GeoPoint pt3 = new GeoPoint(lat, lon);  
  169.         mLat = 39.89923;  
  170.         mLon = 116.367428;  
  171.         lat = (int) (mLat * 1E6);  
  172.         lon = (int) (mLon * 1E6);  
  173.         GeoPoint pt4 = new GeoPoint(lat, lon);  
  174.         mLat = 39.91923;  
  175.         mLon = 116.387428;  
  176.         lat = (int) (mLat * 1E6);  
  177.         lon = (int) (mLon * 1E6);  
  178.         GeoPoint pt5 = new GeoPoint(lat, lon);  
  179.   
  180.         // 构建多边形  
  181.         Geometry polygonGeometry = new Geometry();  
  182.         // 设置多边形坐标  
  183.         GeoPoint[] polygonPoints = new GeoPoint[5];  
  184.         polygonPoints[0] = pt1;  
  185.         polygonPoints[1] = pt2;  
  186.         polygonPoints[2] = pt3;  
  187.         polygonPoints[3] = pt4;  
  188.         polygonPoints[4] = pt5;  
  189.         polygonGeometry.setPolygon(polygonPoints);  
  190.         // 设置多边形样式  
  191.         Symbol polygonSymbol = new Symbol();  
  192.         Symbol.Color polygonColor = polygonSymbol.new Color();  
  193.         polygonColor.red = 0;  
  194.         polygonColor.green = 0;  
  195.         polygonColor.blue = 255;  
  196.         polygonColor.alpha = 126;  
  197.         polygonSymbol.setSurface(polygonColor, 15);  
  198.         // 生成Graphic对象  
  199.         Graphic polygonGraphic = new Graphic(polygonGeometry, polygonSymbol);  
  200.         return polygonGraphic;  
  201.     }  
  202.   
  203.     /** 
  204.      * 绘制单点,该点状态不随地图状态变化而变化 
  205.      *  
  206.      * @return 点对象 
  207.      */  
  208.     public Graphic drawPoint() {  
  209.         double mLat = 39.98923;  
  210.         double mLon = 116.397428;  
  211.         int lat = (int) (mLat * 1E6);  
  212.         int lon = (int) (mLon * 1E6);  
  213.         GeoPoint pt1 = new GeoPoint(lat, lon);  
  214.   
  215.         // 构建点  
  216.         Geometry pointGeometry = new Geometry();  
  217.         // 设置坐标  
  218.         pointGeometry.setPoint(pt1, 10);  
  219.         // 设定样式  
  220.         Symbol pointSymbol = new Symbol();  
  221.         Symbol.Color pointColor = pointSymbol.new Color();  
  222.         pointColor.red = 0;  
  223.         pointColor.green = 126;  
  224.         pointColor.blue = 255;  
  225.         pointColor.alpha = 255;  
  226.         pointSymbol.setPointSymbol(pointColor);  
  227.         // 生成Graphic对象  
  228.         Graphic pointGraphic = new Graphic(pointGeometry, pointSymbol);  
  229.         return pointGraphic;  
  230.     }  
  231.   
  232.     /** 
  233.      * 绘制圆,该圆随地图状态变化 
  234.      *  
  235.      * @return 圆对象 
  236.      */  
  237.     public Graphic drawCircle() {  
  238.         double mLat = 39.90923;  
  239.         double mLon = 116.447428;  
  240.         int lat = (int) (mLat * 1E6);  
  241.         int lon = (int) (mLon * 1E6);  
  242.         GeoPoint pt1 = new GeoPoint(lat, lon);  
  243.   
  244.         // 构建圆  
  245.         Geometry circleGeometry = new Geometry();  
  246.   
  247.         // 设置圆中心点坐标和半径  
  248.         circleGeometry.setCircle(pt1, 2500);  
  249.         // 设置样式  
  250.         Symbol circleSymbol = new Symbol();  
  251.         Symbol.Color circleColor = circleSymbol.new Color();  
  252.         circleColor.red = 0;  
  253.         circleColor.green = 255;  
  254.         circleColor.blue = 0;  
  255.         circleColor.alpha = 126;  
  256.         circleSymbol.setSurface(circleColor, 13);  
  257.         // 生成Graphic对象  
  258.         Graphic circleGraphic = new Graphic(circleGeometry, circleSymbol);  
  259.         return circleGraphic;  
  260.     }  
  261.   
  262.     /** 
  263.      * 绘制文字,该文字随地图变化有透视效果 
  264.      *  
  265.      * @return 文字对象 
  266.      */  
  267.     public TextItem drawText() {  
  268.         double mLat = 39.86923;  
  269.         double mLon = 116.397428;  
  270.         int lat = (int) (mLat * 1E6);  
  271.         int lon = (int) (mLon * 1E6);  
  272.         // 构建文字  
  273.         TextItem item = new TextItem();  
  274.         // 设置文字位置  
  275.         item.pt = new GeoPoint(lat, lon);  
  276.         // 设置文件内容  
  277.         item.text = "百度地图SDK";  
  278.         // 设文字大小  
  279.         item.fontSize = 40;  
  280.         Symbol symbol = new Symbol();  
  281.         Symbol.Color bgColor = symbol.new Color();  
  282.         // 设置文字背景色  
  283.         bgColor.red = 0;  
  284.         bgColor.blue = 0;  
  285.         bgColor.green = 255;  
  286.         bgColor.alpha = 50;  
  287.   
  288.         Symbol.Color fontColor = symbol.new Color();  
  289.         // 设置文字着色  
  290.         fontColor.alpha = 255;  
  291.         fontColor.red = 0;  
  292.         fontColor.green = 0;  
  293.         fontColor.blue = 255;  
  294.         // 设置对齐方式  
  295.         item.align = TextItem.ALIGN_CENTER;  
  296.         // 设置文字颜色和背景颜色  
  297.         item.fontColor = fontColor;  
  298.         item.bgColor = bgColor;  
  299.         return item;  
  300.     }  
  301.   
  302.     @Override  
  303.     protected void onPause() {  
  304.         mMapView.onPause();  
  305.         super.onPause();  
  306.     }  
  307.   
  308.     @Override  
  309.     protected void onResume() {  
  310.         mMapView.onResume();  
  311.         super.onResume();  
  312.     }  
  313.   
  314.     @Override  
  315.     protected void onDestroy() {  
  316.         mMapView.destroy();  
  317.         super.onDestroy();  
  318.     }  
  319.   
  320.     @Override  
  321.     protected void onSaveInstanceState(Bundle outState) {  
  322.         super.onSaveInstanceState(outState);  
  323.         mMapView.onSaveInstanceState(outState);  
  324.   
  325.     }  
  326.   
  327.     @Override  
  328.     protected void onRestoreInstanceState(Bundle savedInstanceState) {  
  329.         super.onRestoreInstanceState(savedInstanceState);  
  330.         mMapView.onRestoreInstanceState(savedInstanceState);  
  331.     }  
  332. }  

布局XMl:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <LinearLayout  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:orientation="horizontal" >  
  11.   
  12.         <Button  
  13.             android:id="@+id/btn_clear"  
  14.             android:layout_width="match_parent"  
  15.             android:layout_height="match_parent"  
  16.             android:layout_margin="10dp"  
  17.             android:layout_weight="1.0"  
  18.             android:background="@drawable/button_style"  
  19.             android:text="清除(clear)" />  
  20.   
  21.         <Button  
  22.             android:id="@+id/btn_reset"  
  23.             android:layout_width="match_parent"  
  24.             android:layout_height="match_parent"  
  25.             android:layout_margin="10dp"  
  26.             android:layout_weight="1.0"  
  27.             android:background="@drawable/button_style"  
  28.             android:text="重置(reset)" />  
  29.     </LinearLayout>  
  30.   
  31.     <com.baidu.mapapi.map.MapView  
  32.         android:id="@+id/bmapView"  
  33.         android:layout_width="match_parent"  
  34.         android:layout_height="match_parent"  
  35.         android:clickable="true" />  
  36.   
  37. </LinearLayout>  

Manifest配置跟前面一样。

附上图片效果:

0 0
原创粉丝点击