I am trying to draw a filled polygon on a map view in my app. No matter what I have tried it will not draw filled in. I can get the strokes to show up but I can not get it to fill. Below is the draw method of my polygon class. It overrides overlays.
public void draw(Canvas canvas, MapView mapv, boolean shadow) {
super.draw(canvas, mapv, shadow);
Paint mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(Color.BLACK);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(6);
Paint mPaintFill = new Paint();
mPaintFill.setStyle(Paint.Style.FILL);
mPaintFill.setColor(Color.GREEN);
Path path = new Path();
GeoPoint start = route.get(0);
for (int i = 1; i < route.size(); ++i) {
Point p1 = new Point();
Point p2 = new Point();
Projection projection = mapv.getProjection();
projection.toPixels(start, p1);
projection.toPixels(route.get(i), p2);
path.moveTo(p2.x, p2.y);
path.lineTo(p1.x, p1.y);
start = route.get(i);
}
canvas.drawPath(path, mPaint);
canvas.drawPath(path, mPaintFill);
//canvas.clipPath(path, Op.DIFFERENCE);
}
The above code for me will draw the lines correctly but it doesn't get filled in. Removing canvas.drawPath(path, mPaint); and just leaving the fill one results in nothing showing on the map. I have even tried setting Paint.Style.STROKE to Paint.Style.FILL_AND_STROKE. I am at a complete loss and at this point am thinking it is something simple I am over looking.
Related
public PNGOverlay(Bitmap original, GeoPoint topLeftGeoPoint, GeoPoint bottomRightGeoPoint) {
this.original = Bitmap.createScaledBitmap(original, original.getWidth(), original.getHeight(), true);
...
topGeoPoint = topLeftGeoPoint;
bottomGeoPoint = bottomRightGeoPoint;
}
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, false);
Projection projection = mapView.getProjection();
Point leftTop = new Point();
Point rightTop = new Point();
Point rightBottom = new Point();
Point leftBottom = new Point();
projection.toPixels(topGeoPoint, leftTop);
projection.toPixels(new GeoPoint(topGeoPoint.getLatitudeE6(), bottomGeoPoint.getLongitudeE6()), rightTop);
projection.toPixels(bottomGeoPoint, rightBottom);
projection.toPixels(new GeoPoint(bottomGeoPoint.getLatitudeE6(), topGeoPoint.getLongitudeE6()), leftBottom);
....
Paint paint = new Paint();
paint.setFilterBitmap(true);
paint.setAntiAlias(true);
canvas.drawBitmap(original, null, new Rect(leftTop.x, leftTop.y, rightBottom.x, rightBottom.y), paint);
....
}
I have a problem with this code. When I draw the png on the mapView, the png image is showed over the compass and over the GPS position indicatior? Any idea?
From your question is difficult to guess what is exactly your problem, but if you mean that the PNG is being drawn over the other overlays and you want it to be under them, you can solve it by adding the PNG overlay first to the mapview.
MapView calls the draw method from each overlay by the order that you add them:
mapview.getoverlays().add(overlay1);
mapview.getoverlays().add(overlay2);
This results in overlay2 being drawn over overlay1.
mapview.getoverlays().add(overlay2);
mapview.getoverlays().add(overlay1);
This results in overlay1 being drawn over overlay2.
good luck.
I am implementing an app using Google Maps. When the app comes into the foreground I am getting the current position lat,long and push pin on the map.
But I require that when I am moving, I want to draw a root path based on my movement.
If any one knows the solution, please help me.
Thanks in advance.
Have you tried to make a class which exends Overlay?
class MyOverlay extends Overlay{
public MyOverlay(){
}
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);
Point p1 = new Point();
Point p2 = new Point();
Path path = new Path();
projection.toPixels(gP1, p1);
projection.toPixels(gP2, p2);
path.moveTo(p2.x, p2.y);
path.lineTo(p1.x,p1.y);
canvas.drawPath(path, mPaint);
}
Use it this way:
private List<Overlay> mapOverlays;
private Projection projection;
mapOverlays = mapView.getOverlays();
projection = mapView.getProjection();
mapOverlays.add(new MyOverlay());
Hope this helps!
i have done this:
public class LongTravlOverlay extends Overlay{
private Projection projection;
private List<GeoPoint> glist;
GeoPoint gP1;
GeoPoint gP2;
public LongTravlOverlay(Projection a,List<GeoPoint> b){
projection=a;
this.glist=b;
}
public void draw(Canvas canvas, MapView mapv, boolean shadow){
super.draw(canvas, mapv, shadow);
Paint mPaint = new Paint();
mPaint.setDither(true);
mPaint.setColor(Color.BLUE);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(3);
for(GeoPoint g1:glist)
{
if(gP1!=null)
{
gP1=gP2;
gP2=new GeoPoint(g1.getLatitudeE6(),g1.getLongitudeE6());
}
else
{
gP1=new GeoPoint(g1.getLatitudeE6(),g1.getLongitudeE6());
gP2=gP1;
continue;
}
Point p1 = new Point();
Point p2 = new Point();
Path path = new Path();
projection.toPixels(gP1, p1);
projection.toPixels(gP2, p2);
path.moveTo(p2.x, p2.y);
path.lineTo(p1.x,p1.y);
canvas.drawPath(path, mPaint);
}
}
}
It works and draws the line between all the points.
The only problem is between the first point and the last point there is also a line that ends up crossing on the path of the other points.
How do i remove it?
You have your geopoints gP1 and gP2 as instance variables. draw will be called multiple times.
At the end of your iterations on the first call to draw, gP2 is the last point. At the first iteration of the second and subsequent calls to draw, gP1 is assigned to the last point and gP2 is assigned to your first point. This draws a line between the first and last point. To rectify this, just declare those variables in draw or initialize it to null before iterating through your geopoints.
gP1 = null;
gP2 = null;
for(GeoPoint g1:glist) {
//Drawing logic
}
I want to draw Driving direction Route from source geopoints to Destination geopoints. I tried this with the below code but its draw a straight line between the Location not proper shortest route.
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
// TODO Auto-generated method stub
super.draw(canvas, mapView, shadow);
Projection projection = classMapView.getProjection();
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(22716221,75896816);
GeoPoint gP2 = new GeoPoint(22715212, 75895806);
Point p1 = new Point();
Point p2 = new Point();
Path path = new Path();
projection.toPixels(gP1, p1);
projection.toPixels(gP2, p2);
path.moveTo(p2.x, p2.y);
path.lineTo(p1.x,p1.y);
canvas.drawPath(path, mPaint);
}
Please help and also tell me is it possible to get text of direction as Google Maps provide.
Please go through Google Policies
This states that turn-by-
turn navigation is not allowed using android MapView.
instead you can use intent to do that as follows
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr=<start lat>,<start lon>&daddr=<dest lat>,<dest lon>"));
startActivity(intent);
Here is a complete source code at https://github.com/frogermcs/RoutePathExample for how to draw path between two geolocation.
I'm having troubles on a simple task.
I'm trying to draw a square at the top left of the map shown below. (top left of the map projection)
But when I convert pixel 0,0 to latitude and longitude with TileSystem (TileSystem.PixelXYToLatLong) the point looks like it is drawn on the screen and not on the map... Why?
The code:
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
// Translate point to x y coordinates on the screen
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
Point point1_draw = new Point();
Point point2_draw = new Point();
Point point3_draw = new Point();
Point point4_draw = new Point();
mapView.getProjection().toPixels(geoPointFromScreenCoords(0,0,mapView),
point1_draw);
mapView.getProjection().toPixels(geoPointFromScreenCoords(0,100,mapView),
point2_draw);
mapView.getProjection().toPixels(geoPointFromScreenCoords(100,0,mapView),
point3_draw);
mapView.getProjection().toPixels(geoPointFromScreenCoords(100,100,mapView),
point4_draw);
Path path = new Path();
path.setFillType(Path.FillType.EVEN_ODD);
path.moveTo(point1_draw.x, point1_draw.y);
path.lineTo(point2_draw.x, point2_draw.y);
path.lineTo(point3_draw.x, point3_draw.y);
path.lineTo(point4_draw.x, point4_draw.y);
path.lineTo(point1_draw.x, point1_draw.y);
path.close();
//canvas.drawCircle(120, 20, 15, paint);
canvas.drawPath(path, paint);
super.draw(canvas, mapView, false);
}`
I'm extending ItemizedOverlay. And this path is drawn on the screen and not on the map. So when i scrol away this path moves with the map... And I dont want that.
Why are you overriding draw instead of addItem?
Here is a video on how to draw on a map