实现结果如下:
代码如下:
- package com.easyway.polyline;
- import java.util.ArrayList;
- import java.util.List;
- import android.os.Bundle;
- import com.google.android.maps.GeoPoint;
- import com.google.android.maps.MapActivity;
- import com.google.android.maps.MapController;
- import com.google.android.maps.MapView;
- /**
- * 自定义的折现图层的实现
- * 在特定情况下,可能需要我们开发人员绘制特定的图层在google map上面显示
- *
- *
- *
- * @author longgangbai
- *
- */
- public class GooglePolylineActivity extends MapActivity {
- private MapView mapView;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mapView =(MapView)findViewById(R.id.mapView);
- //存储连接的点的信息
- List<GeoPoint> points = new ArrayList<GeoPoint>();
- points.add(new GeoPoint(39907794,116356694));
- points.add(new GeoPoint(39950181,116415059));
- points.add(new GeoPoint(39909637,116435315));
- points.add(new GeoPoint(39902526,116398236));
- //添加自定义的图层
- PolyLine polyline = new PolyLine(points);
- mapView.getOverlays().add(polyline); //map是MapView类型
- mapView.invalidate();
- mapView.setBuiltInZoomControls(true);
- MapController mapcontroller=mapView.getController();
- GeoPoint point=new GeoPoint(39950181,116415059);
- mapcontroller.setCenter(point);
- mapcontroller.setZoom(7);
- }
- /**
- * 是否显示路线显示
- */
- @Override
- protected boolean isRouteDisplayed() {
- return true;
- }
- }
- package com.easyway.polyline;
- import java.util.List;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Point;
- import com.google.android.maps.GeoPoint;
- import com.google.android.maps.MapView;
- import com.google.android.maps.Overlay;
- import com.google.android.maps.Projection;
- /**
- * Goolge地图之Polyline实现Overlay 以图层形式存在.
- * MyLocationOverlay:定位当前位置的图层
- * ItemizedOverlay:图层的基类
- * OverlayItem:图层的项
- *
- *
- * @author longgangbai
- */
- public class PolyLine extends Overlay {
- List<GeoPoint> points;
- Paint paint;
- /**
- * 构造函数,使用GeoPoint List构造Polyline
- *
- * @param points
- * GeoPoint点List
- */
- public PolyLine(List<GeoPoint> points) {
- this.points = points;
- paint = new Paint();
- paint.setColor(Color.BLUE);
- paint.setAlpha(150);
- paint.setAntiAlias(true);
- paint.setStyle(Paint.Style.FILL_AND_STROKE);
- paint.setStrokeWidth(4);
- }
- /**
- * 使用GeoPoint点List和Paint对象来构造Polyline
- *
- * @param points
- * GeoPoint点List,所有的拐点
- * @param paint
- * Paint对象,用来控制划线样式
- */
- public PolyLine(List<GeoPoint> points, Paint paint) {
- this.points = points;
- this.paint = paint;
- }
- /**
- * 真正将线绘制出来 只需将线绘制到canvas上即可,主要是要转换经纬度到屏幕坐标
- */
- @Override
- public void draw(Canvas canvas, MapView mapView, boolean shadow) {
- if (!shadow) {// 不是绘制shadow层
- Projection projection = mapView.getProjection();
- if (points != null) {
- if (points.size() >= 2) {
- Point start = projection.toPixels(points.get(0), null);// 需要转换坐标
- for (int i = 1; i < points.size(); i++) {
- Point end = projection.toPixels(points.get(i), null);
- canvas.drawLine(start.x, start.y, end.x, end.y, paint);// 绘制到canvas上即可
- start = end;
- }
- }
- }
- }
- }
- }