I'm using the osmdroid 3.0.5 jar in my app to display a map view. I'm superimposing an overlay, consisting of horizontal and vertical lines. I've noticed that in certain locations only, at high zoom levels some of the lines disappear, then reappear as the map is dragged.
I've produced a minimal sample app which demonstrates the problem which exhibits itself both on a real device and an emulator (Gingerbread 2.3.3).
The complete code is shown below - (the 'transform' methods are necessary in the real app although they don't appear to be in this minimal sample) :
public class DemoMap extends Activity implements MapViewConstants {
private MapView mapView;
private MapController mapController;
private MapOverlay mmapOverlay = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.copymain);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setTileSource(TileSourceFactory.MAPNIK);
mapView.setBuiltInZoomControls(true);
mapView.setMultiTouchControls(true);
mapController = mapView.getController();
mapController.setZoom(17);
// Only shows the bug at ceratin lat/lon positions, this is one
GeoPoint point2 = new GeoPoint(39191699, -120102561);
mapController.setCenter(point2);
mmapOverlay = new MapOverlay(this);
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.add(mmapOverlay);
mapView.invalidate();
}
public class MapOverlay extends org.osmdroid.views.overlay.Overlay {
public MapOverlay(Context ctx) {super(ctx); }
private int mVpl;// viewport left, top, right, bottom
private int mVpt;
private int mVpr;
private int mVpb;
private MapView mMv = null;
// Two routines to transform and scale between viewport and mapview
private float transformX(float in, MapView mv) {
float out;
out = ((mVpr - mVpl) * in) / (mv.getRight() - mv.getLeft())
+ mVpl;
return out;
}
private float transformY(float in, MapView mv) {
float out;
out = ((mVpb - mVpt) * in) / (mv.getBottom() - mv.getTop())
+ mVpt;
return out;
}
#Override
protected void draw(Canvas pC, MapView mapV, boolean shadow) {
if (shadow)
return;
Paint paint;
paint = new Paint();
paint.setColor(Color.RED);
paint.setAntiAlias(true);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(1);
paint.setTextAlign(Paint.Align.LEFT);
paint.setTextSize(12);
final Rect viewportRect = new Rect();
final Projection projection = mapV.getProjection();
viewportRect.set(projection.getScreenRect());
mVpl = viewportRect.left;
mVpt = viewportRect.top;
mVpr = viewportRect.right;
mVpb = viewportRect.bottom;
// draw two lines to split screen into 2x2 quarters
// drag the map left and right and the vertical line disappears,
// then reappears! It's OK at one less zoom level
pC.drawLine(transformX(mapV.getWidth()/2, mapV), transformY(0, mapV),
transformX(mapV.getWidth()/2, mapV),
transformY(mapV.getHeight(), mapV), paint);
pC.drawLine(transformX(0, mapV), transformY(mapV.getHeight()/2, mapV),
transformX(mapV.getWidth(), mapV),
transformY(mapV.getHeight()/2, mapV), paint);
}
}
}
Interestingly if, in my real app, if I do the drawing to an offscreen bitmap, then draw it all in one go at the end of the Overlay's draw, it's OK the lines don't disappear.
Any help will be much appreciated.
I know this is an old question, but I believe what you're experiencing is Issue 221. This has been fixed in version 3.0.9.
Also see: OSMDroid PathOverlay drawing is corrupted at high zoom levels
Sounds like it may be another manifestation of this bug:
Issue 207
Do you see the same behaviour with earlier API versions?
Related
I'm using an Overlay to mark areas on Google Maps by drawing a shape of ten thousands of GeoPoints I get from any source. This works and looks like this:
#Override
public void draw(android.graphics.Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, false);
Projection projection = mapView.getProjection();
List<Zone> zones = ApplicationContext.getZones();
path.rewind();
for (Zone zone : zones) {
paint.setDither(true);
paint.setStyle(Style.FILL);
paint.setAlpha(40);
MultiPolygon multiPolygon = zone.getMultiPolygon();
List<Polygon> polygons = multiPolygon.getPolygons();
for (Polygon polygon : polygons) {
for (List<Coordinate> coordinates : polygon.getCoordinates()) {
for (int i = 0; i < coordinates.size(); i++) {
Point p = new Point();
projection.toPixels(new GeoPoint((int)(coordinates.get(i).getLatitude() * 1E6), (int)(coordinates.get(i).getLongitude() * 1E6)), p);
if (i == 0) {
path.moveTo(p.x, p.y);
}
else {
path.lineTo(p.x, p.y);
}
}
}
}
}
canvas.drawPath(path, paint);
}
The problem is that this is very resource consuming. Every time one scrolls or moves the map on MapView, the path has to be calculated over and over again, because the pixel coordinates have been changed. The drawn area could become so big that the scrolling on the MapView is so slow that it is functional unusable.
My ideas are
to somehow cache the "shape" the path generates and just redraw it
when the zoom level changes on the MapView.
to somehow draw the painting on an "on the fly"-Bitmap to use it as Overlay (maybe as ItemizedOverlay), listen for MapView scrolling and move the bitmap by the scrolled distance.
I'm not sure if there are better methods.
Any ideas how I could solve this problem?
(I'm using Google Maps API 1 and can't change).
Before resorting to trying to figure out how to match the map's movement, there are some optimizations to your current code that will probably yield significant savings. In particular, these two lines inside your inner loop is executed the most times, but fairly expensive to execute (two memory allocations, floating point multiplies, and four method calls).
Point p = new Point();
projection.toPixels(new GeoPoint((int)(coordinates.get(i).getLatitude() * 1E6), (int)(coordinates.get(i).getLongitude() * 1E6)), p);
First, you only ever need one Point object, so avoid allocating it in your loop. Move it to just below your path.rewind();
Second, if you pre-computed your coordinates as GeoPoints instead of computing them each time, you would save a lot of processing in your draw routine. You can also get rid of that if statement with a little work. Assuming you preconvert your list of coordinate to a list of GeoPoint, and make it available through polygon.getGeoCoordinates(), you could end up with your inner loops looking like -
for (List<GeoPoint> geoCoordinates : polygon.getGeoCoordinates()) {
projection.toPixels(geoCoordinates.get(0),p);
path.moveTo(p.x, p.y); // move to first spot
final List<GeoPoint> lineToList = geoCoordinates.sublist(1,geoCoordinates.size()); // A list of all the other points
for(GeoPoint gp : lineToList) {
projection.toPixels(gp, p);
path.lineTo(p.x, p.y);
}
}
And that will run a lot faster than what you were doing before.
After tinkering around in the last days I found a possible solution (and I don't think there is a better one) to not draw the path over and over again but move it to the current position.
The difficult part was to figure out how to cache the drawn shape to not calculate it over and over again. This can be done by using a Matrix. With this Matrix (I imagine this as some kind of "template") you can manipulate the points coordinates inside the path. The first time (when someone starts moving the Map) I draw the area as usual. When it tries to calculate it the second time or more, I don't redraw the shape but I manipulate the path by calculating the "delta" from the current point to the last point. I know what the current point is, because I always map the original GeoPoint (which always stays the same) to the point which results from the current projection. The "delta" needs to be set as Matrix. After that I transform the path by using this new Matrix. The result is really very fast. The scrolling of the Map is as fast as without using an Overlay.
This looks like this (this is no production code, and it cannot deal with zooming yet, but it shows the principle I use as basis for my optimizations):
public class DistrictOverlay extends Overlay {
// private final static String TAG = DistrictOverlay.class.getSimpleName();
private Paint paint = new Paint();
private Path path = new Path();
private boolean alreadyDrawn = false;
private GeoPoint origGeoPoint;
Point p = new Point();
Point lastPoint = new Point();
#Override
public void draw(android.graphics.Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, false);
Projection projection = mapView.getProjection();
List<Zone> zones = ApplicationContext.getZones();
if (!alreadyDrawn) {
path.rewind();
for (Zone zone : zones) {
if (!zone.getZoneId().equals(MenuContext.getChosenZoneId())) {
continue;
}
String dateString = zone.getEffectiveFrom().trim().replace("CEST", "").replace("GMT", "").replace("CET", "").replace("MESZ", "");
if (DateUtil.isBeforeCurrentDate(dateString)) {
paint.setColor(Color.RED);
} else {
paint.setColor(Color.GREEN);
}
paint.setDither(true);
paint.setStyle(Style.FILL);
paint.setAlpha(40);
MultiPolygon multiPolygon = zone.getMultiPolygon();
List<Polygon> polygons = multiPolygon.getPolygons();
for (Polygon polygon : polygons) {
for (List<GeoPoint> geoPoints : polygon.getGeoPoints()) {
projection.toPixels(geoPoints.get(0), p);
path.moveTo(p.x, p.y);
origGeoPoint = new GeoPoint(geoPoints.get(0).getLatitudeE6(), geoPoints.get(0).getLongitudeE6());
lastPoint = new Point(p.x, p.y);
final List<GeoPoint> pathAsList = geoPoints.subList(1, geoPoints.size());
for (GeoPoint geoPoint : pathAsList) {
projection.toPixels(geoPoint, p);
path.lineTo(p.x, p.y);
}
}
}
}
}
else {
projection.toPixels(origGeoPoint, p);
Matrix translateMatrix = new Matrix();
translateMatrix.setTranslate(p.x - lastPoint.x, p.y - lastPoint.y);
path.transform(translateMatrix);
lastPoint = new Point(p.x, p.y);
}
canvas.drawPath(path, paint);
if (!path.isEmpty()) {
alreadyDrawn = true;
}
}
#Override
public boolean onTap(GeoPoint p, MapView mapView) {
return true;
}
}
I'm developing an Android aplication that has to show some "places of interest" on a mapview, along with the device current location. This works well.
Also, in my app, the user could "tap" a "place of interest" marker and the aplication would have to draw a route to that marker.
I used Google Directions api to get the route, along with a polyline decoder to get the GeoPoints between the user and the place. For my testing route, google gives me about 200 different GeoPoints.
So, I have a class like this to add those GeoPoints:
public class RouteOverlay extends Overlay {
private GeoPoint gp1;
private GeoPoint gp2;
private int color;
public RouteOverlay(GeoPoint gp1, GeoPoint gp2, int color) {
this.gp1 = gp1;
this.gp2 = gp2;
this.color = color;
}
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
Projection projection = mapView.getProjection();
Paint paint = new Paint();
Point point = new Point();
projection.toPixels(gp1, point);
paint.setColor(color);
Point point2 = new Point();
projection.toPixels(gp2, point2);
paint.setStrokeWidth(5);
paint.setAlpha(120);
canvas.drawLine(point.x, point.y, point2.x, point2.y, paint);
super.draw(canvas, mapView, shadow);
}
}
what I do to draw the route is the following:
1) Detect the onClick event to a marker in the map.
2) From that event, I create a new thread, where I make the call to the Google API.
3) Once I have the result, I parse/convert it in a GeoPoint list.
4) Then I call my drawPath method:
private void drawPath(List<GeoPoint> geoPoints, int color) {
mapOverlays.clear();
mapOverlays.add(myLocationOverlay);
mapOverlays.add(itemizedoverlay);
for (int i = 1; i < geoPoints.size(); i++) {
mapOverlays.add(new RouteOverlay(geoPoints.get(i - 1), geoPoints.get(i), color));
}
mapView.postInvalidate();
5) Finally, I return to the UI thread.
This method clears the map overlay list (mapOverlays). Then, adds to the list the current location and the "places of interest" overlays. And, finally, adds the route overlays.
The problem is that, suddenly, works veeery slow and finally crashes. But there is no message in the LogCat. So, I thought that 30 overlays + 1 + more than 200 for the route are too much for the phone to handle. But the tutorials I've seen do it this way so...
Can someone tell me if I do anything wrong?
Thanks in advance.
I figured out what I was doing wrong.
When I called the drawPath function, after obtaining the List of GeoPoints, I made a loop to check the coordinates of each point. Something like
for (int i = 0; i < geoList.size(); i++){
Log.i("GEOPOINT " + i, geoList.get(i).toString());
drawpath(geoList, Color.BLUE);
}
The drawPath function got called N times. So the device crashed. My fault.
Programming at 2:00 am is not good for the code!
I wonder if there is a chance to set a final Overlay in Google Maps. I wanna set my cross hairs this way, so that it isn't moving while zooming.
Code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_view);
initData();
mapView = (MapView) findViewById(R.id.mapView);
mapView.setSatellite(true);
mapView.setTraffic(true);
mapView.setBuiltInZoomControls(true);
controller = mapView.getController();
controller.animateTo(coordinate);
controller.setZoom(18);
// TODO:
CrossHairsOverlay chOverlay = new CrossHairsOverlay();
// Pin
MapOverlay mapOverlay = new MapOverlay();
mapOverlay.setPointToDraw(coordinate);
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
listOfOverlays.add(chOverlay);
}
Cross hairs:
public class CrossHairsOverlay extends Overlay {
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
super.draw(canvas, mapView, shadow);
Projection projection = mapView.getProjection();
Point centerPoint = projection.toPixels(mapView.getMapCenter(), null);
Bitmap crossHairs = BitmapFactory.decodeResource(getResources(), R.drawable.cross_hair);
canvas.drawBitmap(crossHairs, centerPoint.x - (int)(27 * dpi + 0.5f), centerPoint.y - (int)(27 * dpi + 0.5f), new Paint());
return true;
}
}
If I use it this way, it recenters after zooming is finished. Left/right draging works correct. I don't really wanna set a "ZoomListener" and redraw all the time. I hope there is a bether solution to do this.
Thanks
Forgive me for not fully understanding your question...
But, to draw to a simple point no need to use the projection, just draw your bitmap strictly to the center of the mapview.
ie...
// make the coordinated constant also.
canvas.drawBitam(bitmap, center.x-(imageWidth/2), center.y-(imageHeight/2),new Paint());
where center.x and center.y are just equal to
Point center = new Point(canvas.getWidth()/2,canvas.getHeight()/2);
Also dont reload your bitmap on every onDraw!, memory leak ftl ;).
I need to draw text on a custom marker which are quite a lot in number. But the problem is that when I draw the text in the Overridden on draw, all the overlay items text appears to be in one layer above the markers and does not seem synched when the map is zoomed in or zoomed out. Previously I was calling the populate when each item was added and this was working fine but it was too slow. Any help?
In my custom class that extends from ItemizedOverlay, the constructor is as follows, where I set the custom marker:
public HotelMapFilterOverlay(Drawable defaultMarker, final Context con) {
super(boundCenterBottom(defaultMarker));
this.marker = defaultMarker;
this.con = con;
}
and the overridden draw method is as follows:
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, shadow);
Bitmap bmp = ((BitmapDrawable) marker).getBitmap();
Paint textPaint = new Paint();
textPaint.setColor(Color.WHITE);
textPaint.setTextSize(16f);
textPaint.setTypeface(Typeface.create(Typeface.SANS_SERIF, 1));
textPaint.setStyle(Paint.Style.FILL);
Point screenPts = new Point();
float bmpWidth = bmp.getWidth();
float bmpHeight = bmp.getHeight();
float left;
float top;
int total = mOverlayItems.size();
for (int index = 0; index < total; index++) {
gp = mOverlayItems.get(index).getPoint();
mapView.getProjection().toPixels(gp, screenPts);
left = screenPts.x - (bmpWidth / 2);
top = screenPts.y - bmpHeight;
// draw text
this.title = mOverlayItems.get(index).getTitle();
canvas.drawText(this.title, left + 8, top + 30,textPaint);
}
}
and I calling the populate only once to make this efficient like
public void callPopulate(){
populate();
}
To get your marker text to appear separately over each marker without all of it layering on top of the markers, you need to draw the markers yourself and remove super.draw(canvas, mapView, shadow).
To draw the markers you have most of the code already! Just create a new Paint markerPaint = new Paint() object and add the following in your for-loop before you draw your text:
canvas.drawBitmap(bmp, screenPts.x-(bmp.getWidth()/2), screenPts.y-(bmp.getHeight()), markerPaint);
This way the marker is drawn then its text is drawn on top.
With respect to 'populate' you should call this just after you add all the items to your ItemizedOverlay.
I am currently drawing lines on a MapView based on different GeoPoints to indicate sectors. With the following code (this is within an overlay):
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow)
{
for(Polygon polygonTemp : polygonList)
{
Path p = new Path();
Projection projection = mapView.getProjection();
boolean firstTime = true;
for(GeoPoint geoPoint : polygonTemp.getGeoPointList())
{
Point drawPoint = new Point();
projection.toPixels(geoPoint, drawPoint);
if(firstTime)
{
p.moveTo(drawPoint.x, drawPoint.y);
firstTime = false;
}
else
{
p.lineTo(drawPoint.x, drawPoint.y);
}
}
p.setFillType(Path.FillType.EVEN_ODD);
Paint polyPaint = new Paint();
polyPaint.setStrokeWidth(1);
polyPaint.setStyle(Paint.Style.FILL_AND_STROKE);
polyPaint.setAntiAlias(true);
polyPaint.setColor(Color.parseColor(polygonTemp.getColor()));
canvas.drawPath(p, polyPaint);
firstTime = true;
}
super.draw(canvas, mapView, shadow);
}
The problem is, I want them to be filled with some degree of transparency, so I can still see the map under the filled sectors. I tried to set polyPaint.setAlpha(), even to 255 (which should be completely transparent) and it doesn't do anything, it's completely opague.
Anyone knows what I'm doing wrong?
I don't see where you are setting the alpha. Regardless 255 is not transparent, it is opaque.
FYI, I am doing identical things (drawing paths on map overlays) and this works fine for drawing a 50% opaque, red line:
mPaint.setColor(Color.parseColor ("#88ff0000"));