Android google map自定义的折现图层的实现

实现结果如下:

 

代码如下: 

Java代码  收藏代码
  1. package com.easyway.polyline;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.os.Bundle;  
  7.   
  8. import com.google.android.maps.GeoPoint;  
  9. import com.google.android.maps.MapActivity;  
  10. import com.google.android.maps.MapController;  
  11. import com.google.android.maps.MapView;  
  12. /** 
  13.  * 自定义的折现图层的实现 
  14.  *  在特定情况下,可能需要我们开发人员绘制特定的图层在google map上面显示 
  15.  *   
  16.  *   
  17.  *  
  18.  * @author longgangbai 
  19.  * 
  20.  */  
  21. public class GooglePolylineActivity extends MapActivity {  
  22.     private MapView mapView;  
  23.       
  24.     /** Called when the activity is first created. */  
  25.     @Override  
  26.     public void onCreate(Bundle savedInstanceState) {  
  27.         super.onCreate(savedInstanceState);  
  28.         setContentView(R.layout.main);  
  29.         mapView =(MapView)findViewById(R.id.mapView);  
  30.           
  31.         //存储连接的点的信息  
  32.         List<GeoPoint> points = new ArrayList<GeoPoint>();  
  33.         points.add(new GeoPoint(39907794,116356694));  
  34.         points.add(new GeoPoint(39950181,116415059));  
  35.         points.add(new GeoPoint(39909637,116435315));  
  36.         points.add(new GeoPoint(39902526,116398236));  
  37.           
  38.         //添加自定义的图层  
  39.         PolyLine polyline = new PolyLine(points);  
  40.           
  41.         mapView.getOverlays().add(polyline); //map是MapView类型  
  42.           
  43.         mapView.invalidate();  
  44.         mapView.setBuiltInZoomControls(true);  
  45.           
  46.         MapController mapcontroller=mapView.getController();  
  47.         GeoPoint  point=new GeoPoint(39950181,116415059);  
  48.         mapcontroller.setCenter(point);  
  49.         mapcontroller.setZoom(7);  
  50.     }  
  51.   
  52.     /** 
  53.      * 是否显示路线显示 
  54.      */  
  55.     @Override  
  56.     protected boolean isRouteDisplayed() {  
  57.         return true;  
  58.     }  
  59.       
  60.       
  61. }  
Java代码  收藏代码
  1. package com.easyway.polyline;  
  2.   
  3.   
  4. import java.util.List;  
  5.   
  6. import android.graphics.Canvas;  
  7. import android.graphics.Color;  
  8. import android.graphics.Paint;  
  9. import android.graphics.Point;  
  10.   
  11. import com.google.android.maps.GeoPoint;  
  12. import com.google.android.maps.MapView;  
  13. import com.google.android.maps.Overlay;  
  14. import com.google.android.maps.Projection;  
  15.   
  16. /** 
  17.  * Goolge地图之Polyline实现Overlay 以图层形式存在. 
  18.  * MyLocationOverlay:定位当前位置的图层 
  19.  * ItemizedOverlay:图层的基类 
  20.  * OverlayItem:图层的项 
  21.  *  
  22.  *  
  23.  * @author longgangbai 
  24.  */  
  25. public class PolyLine extends Overlay {  
  26.     List<GeoPoint> points;  
  27.     Paint paint;  
  28.   
  29.     /** 
  30.      * 构造函数,使用GeoPoint List构造Polyline 
  31.      *  
  32.      * @param points 
  33.      *            GeoPoint点List 
  34.      */  
  35.     public PolyLine(List<GeoPoint> points) {  
  36.         this.points = points;  
  37.         paint = new Paint();  
  38.         paint.setColor(Color.BLUE);  
  39.         paint.setAlpha(150);  
  40.         paint.setAntiAlias(true);  
  41.         paint.setStyle(Paint.Style.FILL_AND_STROKE);  
  42.         paint.setStrokeWidth(4);  
  43.     }  
  44.   
  45.     /** 
  46.      * 使用GeoPoint点List和Paint对象来构造Polyline 
  47.      *  
  48.      * @param points 
  49.      *            GeoPoint点List,所有的拐点 
  50.      * @param paint 
  51.      *            Paint对象,用来控制划线样式 
  52.      */  
  53.     public PolyLine(List<GeoPoint> points, Paint paint) {  
  54.         this.points = points;  
  55.         this.paint = paint;  
  56.     }  
  57.   
  58.   
  59.     /** 
  60.      * 真正将线绘制出来 只需将线绘制到canvas上即可,主要是要转换经纬度到屏幕坐标 
  61.      */  
  62.     @Override  
  63.     public void draw(Canvas canvas, MapView mapView, boolean shadow) {  
  64.         if (!shadow) {// 不是绘制shadow层  
  65.             Projection projection = mapView.getProjection();  
  66.             if (points != null) {  
  67.                 if (points.size() >= 2) {  
  68.                     Point start = projection.toPixels(points.get(0), null);// 需要转换坐标  
  69.                     for (int i = 1; i < points.size(); i++) {  
  70.                         Point end = projection.toPixels(points.get(i), null);  
  71.                         canvas.drawLine(start.x, start.y, end.x, end.y, paint);// 绘制到canvas上即可  
  72.                         start = end;  
  73.                     }  
  74.                 }  
  75.             }  
  76.         }  
  77.     }  

原文地址:https://www.cnblogs.com/shihao/p/2323697.html