gpsandroid-canvasgeometrymap-projectionsnutiteq

I can't draw my current Position on a Nutiteq Map


I try to draw a blue translucent circle to show my actual GPS Position on Nutiteq's Map but it shows no circle.

I want to display something like this enter image description here

My Code is the following Circle Class

public Circle(MapView mapView, MapPos mapPostPoint, float radius, Paint paintFill, Paint paintStroke) {
        checkRadius(radius);
        this.mapView      = mapView;
        this.mapPostPoint = mapPostPoint;
        this.radius       = radius;
        this.paintFill    = paintFill;
        this.paintStroke  = paintStroke;

        this.gUtils = new GeometricUtils(this.mapView);
}

public synchronized boolean draw(MapPos point, Canvas canvas, float radius, float zoomLevel) {
        if (this.mapPostPoint == null || (this.paintStroke == null && this.paintFill == null)) {
                return false;
        }

        double latitude  = point.x;
        double longitude = point.y;

        MapPos screenPoint = mapView.worldToScreen(point.x, point.y, 0);
        float pixelX = (float) screenPoint.x;
        float pixelY = (float) screenPoint.y;


        float radiusInPixel = (float) gUtils.metersToPixels((double)this.radius, latitude, zoomLevel);

        if (this.paintStroke != null) {
            canvas.drawCircle(pixelX, pixelY, radiusInPixel, this.paintStroke);
        }
        if (this.paintFill != null) {
            canvas.drawCircle(pixelX, pixelY, radiusInPixel, this.paintFill);
        }

        return true;
}

with GeometricUtils Class as

...
private static final int tileSize = 256;
...

public double resolution(double latitude, float scaleFactor) {
    long mapSize = getMapSize(scaleFactor, tileSize);
    return Math.cos(latitude * (Math.PI / 180)) * earthCircumference / mapSize;
}   

// Here I get how many Pixels I need to represent a distance of "meters"
public double metersToPixels(double meters, double latitude, float zoom) {
    double res = resolution(latitude, zoom);

    return meters / res;

}   


public long getMapSize(float scaleFactor, int tileSize) {
        if (scaleFactor < 1) {
                throw new IllegalArgumentException("scale factor: " + scaleFactor + " should be >= 1 ");
        }
        return (long) (tileSize * (Math.pow(2, scaleFactorToZoomLevel(scaleFactor))));
}


public double scaleFactorToZoomLevel(double scaleFactor) {
       return Math.log(scaleFactor) / Math.log(2);
} 

And last MyLocationCircle Class

public class MyLocationCircle {

private final   GeometryLayer layer;
private MapView mapView;

private MapPos  circlePos       = new MapPos(0, 0);
private float   circleScale     = 0;
private float   circleRadius    = 1;
private float   projectionScale = 0;
private boolean visible = false;
private Circle  circle;
private Paint   fill            = new Paint();
private Paint   stroke          = new Paint();
private Canvas  canvas          = new Canvas();

public MyLocationCircle(GeometryLayer layer, MapView mapView, double radius) {
    // Initialize Paint
    initializeGraphics();
    //

    this.layer        = layer;
    this.mapView      = mapView;
    this.circleRadius = (float)radius;
    this.circle       = new Circle(this.mapView, this.circlePos, this.circleRadius, this.fill, this.stroke);

}

public void setVisible(boolean visible) {
    this.visible = visible;
}

public void setLocation(Projection proj, Location location) {
    circlePos = new MapPos(location.getLongitude(), location.getLatitude());//proj.fromWgs84(location.getLongitude(), location.getLatitude());

    projectionScale = (float) proj.getBounds().getWidth();
    circleRadius = location.getAccuracy();

}

public void draw(MapPos position) {
    float zoom = mapView.getZoom();
    circle.draw(position, canvas, circleRadius, zoom);      
}

public MapPos getLocation() {
    return circlePos;
}

protected void initializeGraphics() {

    fill.setStyle(Paint.Style.FILL);
    fill.setColor(Color.BLUE);
    fill.setAlpha(60);

    stroke = new Paint();
    stroke.setStrokeWidth(3.0f);        
    stroke.setStyle(Paint.Style.STROKE);
    stroke.setColor(Color.BLUE);

}

}

I turn the geo coordinates right into screen coordinates, because I can reverse right with the method screenToWorld(...) Method and I get right geo coordinates, so, what am I making wrong?

I can draw Polygons, Lines, Markers, Labels, etc so I can attach to Map via right Layers but I can't display a simple Canvas Circle on a Map.

I thank you in advance for your help.


Solution

  • Your code sample misses many important parts. How do you use your Canvas, how it is added to the Layout and how it relates to MapView? What calls circle.draw?

    Nutiteq wiki has GPS circle sample, did you check this? This adds Polygon object to map, and manipulates this using map coordinates, this way you do not need to know anything about screen coordinates, all this is handled by Nutiteq Maps SDK.