0

How to draw a link in google map. I am currently integrating the google map V2 into my application. i can able to get the current location also i can able to get the geo location when the onLocationChange happens. But i want to mark a line among the geo pointers which i gets. is that possible to do with version2. Can you guys pls guide me on this. im pretty new to this topic. Thanks in advance.

 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));

Using this code i can able to mare an image to the latitude and longitude.But i want to draw a line on that.

MaciejGórski
  • 22,187
  • 7
  • 70
  • 94
Ram
  • 83
  • 1
  • 3
  • 11

2 Answers2

1

Going through the Developer Guide really helps tou get started.

Anyway, what you need here is a Polyline.

MaciejGórski
  • 22,187
  • 7
  • 70
  • 94
0

I hope this may help you:- Create new Overlay class as 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;
        }
}

Then call this class in your mapActivity:-

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

path.add(point);

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