I'm trying to add text labels next to my overlay images. So far the only way I can see to do this would be to use the draw method and draw the text as overlay. I did this, but somehow it isn't showing me the drawn text. My code looks like:
SitesOverlay that extends ItemizedOverlay<OverlayItem>
public void draw(Canvas canvas, MapView mapView,boolean shadow) {
int i;
Paint paint=new Paint();
paint.setStrokeWidth(1);
paint.setARGB(255, 255, 255, 255);
paint.setStyle(Paint.Style.STROKE);
super.draw(canvas, mapView, shadow);
boundCenterBottom(station);
canvas.drawText("hullo",28632877,77219722, paint);
}
My constructor in the SitesOverlay class just adds the images to many different GeoPoints.
Now, in my OnCreate I have this piece of code:
map.getOverlays().add(new SitesOverlay(station));
This is adding the list of images in my constructor - SitesOverlay(station) as overlays.
My question is that since I have added my text in the Draw method of the SitesOverlay class and not in this constructor, is this why the text is not being drawn on the map? If so how do I add the text to the map?
Do the things drawn in the draw() method automatically get added as an overlay? Coz i think thats what is causing the problem here...
Any other way I can add text labels next to my overlay images?
Plz help...
try this..
MyLocationOverlay myTouchOverlay = new MyLocationOverlay ();
List<Overlay> list1 = myMapView.getOverlays();
list1.add(myTouchOverlay);
class MyLocationOverlay extends com.google.android.maps.Overlay {
#Override
public boolean onTap(GeoPoint p, MapView mapView) {
}
#Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
super.draw(canvas, mapView, shadow);
Paint paint = new Paint();
// Converts lat/lng-Point to OUR coordinates on the screen.
Point myScreenCoords = new Point();
mapView.getProjection().toPixels(point, myScreenCoords);
paint.setStrokeWidth(1);
paint.setARGB(255, 255, 255, 255);
paint.setStyle(Paint.Style.STROKE);
paint.setTextSize(20);
paint.setColor(Color.RED);
paint.setStrokeWidth(2);
canvas.drawText("Here I am...", myScreenCoords.x-10,myScreenCoords.y-48, paint);
return true;
}
}
Related
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.
I currently need to display a boat/cruise ship route in a MapView, something like this
I started coding my version of it, but since every example that I see of water routes are the same I started wondering if there was a "standard" method of doing it. I have been searching for hours and haven't found anything yet, so I decided to ask here.
Here's what I have so far:
Point point2 = new Point();
projection.toPixels(gp2, point2);
paint.setStrokeWidth(5);
paint.setPathEffect(new DashPathEffect(new float[] {8,12}, 5));
paint.setAlpha(defaultColor==Color.parseColor("#6C8715")?200:100);
canvas.drawLine(point.x, point.y, point2.x,point2.y, paint);
It is working and the only thing left to do is adjust the line every time that the user zooms.
Is this the way to do it, or there's a standard way of doing it?
Any help is appreciated, thank you.
If you move your code to an Overlay then you wouldn't have to worry about adjusting the line when the user zooms/pans. Since you probably have more than two points to draw you can also take advantage of the Path class.
import com.google.android.maps.Overlay;
class PathOverlay extends Overlay {
private void List<GeoPoint>geoPoints;
public PathOverlay(List<GeoPoint> points) {
this.geoPoints = geoPoints;
}
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapv, shadow);
Paint paint = new Paint();
paint.setStrokeWidth(5);
paint.setPathEffect(new DashPathEffect(new float[] {8,12}, 5));
paint.setAlpha(defaultColor==Color.parseColor("#6C8715")?200:100);
Path path = new Path();
boolean isFirst = true;
for (GeoPoint geoPoint : this.geoPoints) {
Point point = new Point();
mapView.getProjection().toPixels(geoPoint, point);
if (isFirst) {
isFirst = false;
path.moveTo(point.x, point.y);
}
else {
path.lineTo(point.x,point.y);
}
}
canvas.drawPath(path, paint);
}
}
Extend Overlay and override draw() method in it. Then do the same you did previously. You will get automatic redraw in move/zoom
This one answer will work for you How to draw a path on a map using kml file?
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.
To show an specific location in android Map, I am using overlay class, and paint object which have reference of Geo point, drawing an balloon bitmap image.
But when I zoom (in/out) map, my balloon does not adjust according to the changed view of the map.
I'm using the following overlay pattern:
class MapOverlay extends com.google.android.maps.Overlay
{
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
Point point = null, point1 = null;
if(p!=null)
{
point = new Point();
mapView.getProjection().toPixels(p, point);
Bitmap bmp = BitmapFactory.decodeResource(
getResources(), R.drawable.redbaloon);
canvas.drawBitmap(bmp, point.x-38, point.y+60, null);
}}
How can I draw balloon with zooming?
i want to mark with push pin image on more than one places on google map using overlayitem class..OR in simple how to add more than one overlay items on map..
as of now i can only mark a single place by overriding draw method of mapoverlay subclass...
here is my code
class MapOverlay extends com.google.android.maps.Overlay
{
#Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pushpin);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-24, null);
return true;
}
}
You can draw as many things as you like in the draw() method so just iterate over all the points you have in a loop and draw them one by one.