Reputation: 101
I am working in Google Maps. I want to create a Path between 5 to 6 nodes, I mean just drawing a line between points.
The following class is an inner class for drawing:
class MyOverlay extends Overlay{
public void draw(Canvas canvas, MapView mapv, boolean shadow){
super.draw(canvas, mapv, shadow);
Paint mPaint = new Paint();
mPaint.setDither(true);
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(2);
GeoPoint gP1 = new GeoPoint(19240000,-99120000);
GeoPoint gP2 = new GeoPoint(37423157, -122085008);
GeoPoint gP3 = new GeoPoint(20000000 , -50000000) ;
GeoPoint[] G = new GeoPoint[3] ;
G[1] = gP1 ;
G[2] = gP2 ;
G[3] = gP3 ;
Point p1 = new Point();
Point p2 = new Point();
Point p3 = new Point () ;
Path path = new Path();
projection.toPixels(gP1, p1);
projection.toPixels(gP2, p2);
projection.toPixels(gP3, p3) ;
//path.moveTo(p2.x, p2.y);
//path.lineTo(p1.x,p1.y);
//path.lineTo(p3.x, p3.y) ;
//canvas.drawLines(pts , mPaint) ;
canvas.drawPath(path, mPaint);
}
}
These two lines of code are the code from the onCreate()
method in the outer class:
projection = mapView.getProjection();
mapOverlays.add(new MyOverlay());
Upvotes: 2
Views: 2551
Reputation: 414
The above answers are correct - the problem is you are not adding a Point
to your Path
. See this answer for something exactly similar to what you're doing. Also, draw()
is executed EVERY time the map needs to redraw the route, which is basically anytime the map moves.I have an app where I draw a route on the MapView
and let me tell you, if your route gets long your map can get laggy. So I would put your Paint
stuff inside your class's constructor, so you're not creating a new object every time the route is redrawn. Also, if you're planning to draw multiple points, I would use a for
loop similar to this:
Path path = new Path();
for(GeoPoint g : arrayOfGeoPoints) {
Point next = new Point();
projection.toPixels(g, next);
path.lineTo(next.x, next.y);
path.moveTo(next.x, next.y);
}
canvas.drawPath(path, myPaint);
It's simple, but can iterate through many points quickly and efficiently.
Good luck!
Upvotes: 1
Reputation: 34031
I'm not familiar with GeoPoint or MapView, so you may have other problems, but I can point out the obvious:
You've commented out the lines where you generate the Path, so you're drawing an empty Path. If you uncomment the three lines starting with path.
(but leave canvas.drawLines(...)
commented), you should have a line from p2
to p1
and another one from p1
to p3
, assuming there isn't anything else wrong.
Upvotes: 0