Hello everyone. I'm having problems with adding lines to the map in my android project. Basically, when I want to draw lines from point A to point B android is painting them just fine, but when I want to add another line later (say from point B to point C) android is removing the old line and drawing a new one. I guess it has something to do with collections because I'm using the ItemizedOverlay class to collect all markers and it seems to work, but how to do the same with lines or anything else I would like to draw? How to prevent android from refreshing the map? ItemizedOverlays seems to do the trick, but only with markers/drawables. Can someone please give me some help or at least point me in the right direction with this? I would greatly appreciate it.
See my reply with code samples at http://stackoverflow.com/questions/3109158/how-to-draw-a-path-on-a-map-using-kml-file/3109723#3109723, it describes how to draw routes (consisting of multiple lines). Especially look at the Drawing / drawPath() section.
Hey Mathias - thanks for the reply. I'm just a beginner in Android world so please bear with me. My code seems to be slightly different from your one. Can you look at this and tell me what you think of it?
This is my button along with method (this is where I'm calculating the boundaries within which I would like to display specific markers from db):
this.zoomOUT = (Button)this.findViewById(R.id.zoomOUT);
this.zoomOUT.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mapView.getOverlays().clear();
mapView.invalidate();
int maxLati = mapView.getLatitudeSpan();
int maxLong = mapView.getLongitudeSpan();
GeoPoint center = mapView.getMapCenter();
int centerLat = center.getLatitudeE6();
int centerLong = center.getLongitudeE6();
int boundariesLatSub = center.getLatitudeE6()-(maxLati/2);
int boundariesLatAdd = center.getLatitudeE6()+(maxLati/2);
int boundariesLongSub = center.getLongitudeE6()-(maxLong/2);
int boundariesLongAdd = center.getLongitudeE6()+(maxLong/2);
//DisplayInfoMessage("Max Lati:" + maxLati +"and MaxLong" + maxLong);
//mapView.getOverlays().clear();
db.open();
Cursor c = db.getAllTitles(); // loop here through db
startManagingCursor(c);
int areasNumber = 0;
if (c != null && c.moveToFirst()) {
do {
Tribo o1 = new Tribo();
o1.setTriboName(c.getString(7));
o1.setTriboLat1(c.getInt(1));
o1.setTriboLong1(c.getInt(2));
o1.setTriboLat2(c.getInt(3));
o1.setTriboLong2(c.getInt(4));
o1.setTriboLat3(c.getInt(5));
o1.setTriboLong3(c.getInt(6));
// DisplayTitle(c.getString(7)+c.getString(1));
//------------
//DisplayInfoMessage("|center-maxLati:" + mapView.getOverlays());
if(o1.getTriboLat1() > boundariesLatSub &&
o1.getTriboLat1() < boundariesLatAdd &&
o1.getTriboLong1() > boundariesLongSub &&
o1.getTriboLong1() < boundariesLongAdd &&
o1.getTriboLat2() > boundariesLatSub &&
o1.getTriboLat2() < boundariesLatAdd &&
o1.getTriboLong2() > boundariesLongSub &&
o1.getTriboLong2() < boundariesLongAdd &&
o1.getTriboLat3() > boundariesLatSub &&
o1.getTriboLat3() < boundariesLatAdd &&
o1.getTriboLong3() > boundariesLongSub &&
o1.getTriboLong3() < boundariesLongAdd &&
areasNumber < 20 //MAXIMUM NUMBER OF AREAS TO DISPLAY ON TILE
)
{
displayCoords(o1.getTriboLat1(),o1.getTriboLong1());
displayCoords(o1.getTriboLat2(),o1.getTriboLong2());
displayCoords(o1.getTriboLat3(),o1.getTriboLong3());
controlPoints = 2;
geoPoint1 = new GeoPoint(
(int) (o1.getTriboLat1()),
(int) (o1.getTriboLong1()));
geoPoint2 = new GeoPoint(
(int) (o1.getTriboLat2()),
(int) (o1.getTriboLong2()));
geoPoint3 = new GeoPoint(
(int) (o1.getTriboLat3()),
(int) (o1.getTriboLong3()));
//Point point1 = (o1.getTriboLat1(), o1.getTriboLong1());
//geoPoint1 = (o1.getTriboLat1(),o1.getTriboLong1());
areasNumber = areasNumber+1;
//DisplayInfoMessage(o1.getTriboName() + areasNumber);
//List<Overlay> mapOverlays = mapView.getOverlays();
MyLocationOverlay myOverlay = new MyLocationOverlay();
mapView.getOverlays().add(myOverlay);
}
//DisplayInfoMessage(o1.getTriboName());
//------------
mTribo.add(o1);
} while (c.moveToNext());
}
db.close();
}
});
And my locationOverlay class:
class MyLocationOverlay extends Overlay
{
//---------------------------------------------------------------
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
{
super.draw(canvas, mapView, true);
Paint paint = new Paint();
paint.setStrokeWidth(2);
paint.setARGB(255, 153, 29, 29);
paint.setStyle(Paint.Style.STROKE);
if(controlPoints == 2)
{
Point point1_returned = new Point();
Point point2_returned = new Point();
Point point3_returned = new Point();
mapView.getProjection().toPixels(geoPoint1, point1_returned);
mapView.getProjection().toPixels(geoPoint2, point2_returned);
mapView.getProjection().toPixels(geoPoint3, point3_returned);
/*Path path = new Path();
path.moveTo(point1_returned.x, point1_returned.y);
path.lineTo(point2_returned.x, point2_returned.y);
path.moveTo(point2_returned.x, point2_returned.y);
path.lineTo(point3_returned.x, point3_returned.y);
path.moveTo(point3_returned.x, point3_returned.y);
path.lineTo(point1_returned.x, point1_returned.y);
canvas.drawPath(path, paint);*/
canvas.drawLine(point1_returned.x, point1_returned.y, point2_returned.x, point2_returned.y, paint);
canvas.drawLine(point2_returned.x, point2_returned.y, point3_returned.x, point3_returned.y, paint);
canvas.drawLine(point3_returned.x, point3_returned.y, point1_returned.x, point1_returned.y, paint);
return true;
}
else if(controlPoints == 1)
{
Point point1_returned = new Point();
Point point2_returned = new Point();
Point point3_returned = new Point();
mapView.getProjection().toPixels(returned_pin1_geo, point1_returned);
mapView.getProjection().toPixels(returned_pin2_geo, point2_returned);
mapView.getProjection().toPixels(returned_pin3_geo, point3_returned);
/*Path path = new Path();
path.moveTo(point1_returned.x, point1_returned.y);
path.lineTo(point2_returned.x, point2_returned.y);
path.moveTo(point2_returned.x, point2_returned.y);
path.lineTo(point3_returned.x, point3_returned.y);
path.moveTo(point3_returned.x, point3_returned.y);
path.lineTo(point1_returned.x, point1_returned.y);
canvas.drawPath(path, paint);
*/
canvas.drawLine(point1_returned.x, point1_returned.y, point2_returned.x, point2_returned.y, paint);
canvas.drawLine(point2_returned.x, point2_returned.y, point3_returned.x, point3_returned.y, paint);
canvas.drawLine(point3_returned.x, point3_returned.y, point1_returned.x, point1_returned.y, paint);
return true;
}
else if (controlPoints == 3)
{
Point point1_returned = new Point();
Point point2_returned = new Point();
Point point3_returned = new Point();
mapView.getProjection().toPixels(pin1_geo, point1_returned);
mapView.getProjection().toPixels(pin2_geo, point2_returned);
mapView.getProjection().toPixels(pin3_geo, point3_returned);
canvas.drawLine(point1_returned.x, point1_returned.y, point2_returned.x, point2_returned.y, paint);
canvas.drawLine(point2_returned.x, point2_returned.y, point3_returned.x, point3_returned.y, paint);
canvas.drawLine(point3_returned.x, point3_returned.y, point1_returned.x, point1_returned.y, paint);
return true;
}
else
{
return false;
}
}
}
And displayCoords method looks like this:
public void displayCoords(int latitude, int longitude)
{
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.pin); //getting pin
GeoPoint point = new GeoPoint(latitude,longitude);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable); //inserting pin in the constructor
itemizedoverlay.addPoint(point);
mapOverlays.add(itemizedoverlay);
OverlayItem overlayitem = new OverlayItem(point, null, null);
itemizedoverlay.addOverlay(overlayitem);
// mc.animateTo(point);
}
Now my question is - how to store those canvases in the map? (i.e to display them all?) I'm talking about this here:
canvas.drawLine(point1_returned.x, point1_returned.y, point2_returned.x, point2_returned.y, paint);
canvas.drawLine(point2_returned.x, point2_returned.y, point3_returned.x, point3_returned.y, paint);
canvas.drawLine(point3_returned.x, point3_returned.y, point1_returned.x, point1_returned.y, paint);
return true;
When I'm running this code right now it only stores markers (through ItemizedOverlay class). Please can you help me with this one?