Android--如何在googlemap-v2中绘制一条线

时间:2013-06-13 09:23:08

标签: java android google-maps google-maps-android-api-2 android-maps-v2

如何在谷歌地图中绘制链接。我目前正在将谷歌地图V2集成到我的应用程序中。我能够获得当前位置,也可以在onLocationChange发生时获取地理位置。但我想在我得到的地理指针中标记一条线。这可能与版本2有关。你们能不能指导我这件事。我对这个话题很新。提前致谢。

 Marker startPerc = googleMap.addMarker(new MarkerOptions() .position(coordinate).title("On the way").snippet("Hello Apps").icon(BitmapDescriptorFactory.fromResource(R.drawable.myicon)));
         //  startPerc.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.my icon));

使用此代码,我可以将图像母马和经度分割。但我想在其上画一条线。

2 个答案:

答案 0 :(得分:1)

通过the Developer Guide确实可以帮助您入门。

无论如何,你需要的是Polyline

答案 1 :(得分:0)

我希望这可以帮到你: - 创建新的Overlay类作为RoutePathOverlay: -

public class RoutePathOverlay extends Overlay {

        private int _pathColor;
        private final List<GeoPoint> _points;
        private boolean _drawStartEnd;

        public RoutePathOverlay(List<GeoPoint> points) {
                this(points, Color.RED, true);
        }

        public RoutePathOverlay(List<GeoPoint> points, int pathColor, boolean drawStartEnd) {
                _points = points;
                _pathColor = pathColor;
                _drawStartEnd = drawStartEnd;
        }

        private void drawOval(Canvas canvas, Paint paint, Point point) {
                Paint ovalPaint = new Paint(paint);
                ovalPaint.setStyle(Paint.Style.FILL_AND_STROKE);
                ovalPaint.setStrokeWidth(2);
                int _radius = 6;
                RectF oval = new RectF(point.x - _radius, point.y - _radius, point.x + _radius, point.y + _radius);
                canvas.drawOval(oval, ovalPaint);               
        }

        public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
                Projection projection = mapView.getProjection();
                if (shadow == false && _points != null) {
                        Point startPoint = null, endPoint = null;
                        Path path = new Path();
                        //We are creating the path
                        for (int i = 0; i < _points.size(); i++) {
                                GeoPoint gPointA = _points.get(i);
                                Point pointA = new Point();
                                projection.toPixels(gPointA, pointA);
                                if (i == 0) { //This is the start point
                                        startPoint = pointA;
                                        path.moveTo(pointA.x, pointA.y);
                                } else {
                                        if (i == _points.size() - 1)//This is the end point
                                                endPoint = pointA;
                                        path.lineTo(pointA.x, pointA.y);
                                }
                        }

                        Paint paint = new Paint();
                        paint.setAntiAlias(true);
                        paint.setColor(_pathColor);
                        paint.setStyle(Paint.Style.STROKE);
                        paint.setStrokeWidth(5);
                        paint.setAlpha(90);
                        if (getDrawStartEnd()) {
                                if (startPoint != null) {
                                        drawOval(canvas, paint, startPoint);
                                }
                                if (endPoint != null) {
                                        drawOval(canvas, paint, endPoint);
                                }
                        }
                        if (!path.isEmpty())
                                canvas.drawPath(path, paint);
                }
                return super.draw(canvas, mapView, shadow, when);
        }

        public boolean getDrawStartEnd() {
                return _drawStartEnd;
        }

        public void setDrawStartEnd(boolean markStartEnd) {
                _drawStartEnd = markStartEnd;
        }
}

然后在mapActivity中调用此类: -

List<GeoPoint> path = new ArrayList<GeoPoint>(); //List of GeoPoints

path.add(point);

mapView.getOverlays().add(new RoutePathOverlay(path));