0

我尝试绘制一个蓝色半透明圆圈以显示我的实际 GPS 位置,Nutiteq's Map但它没有显示圆圈。

我想显示这样的东西 在此处输入图像描述

我的代码是以下 Circle

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;
}

GeometricUtils

...
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);
} 

最后一MyLocationCircle堂课

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);

}

}

我将地理坐标正确地转换为屏幕坐标,因为我可以使用screenToWorld(...)Method 方法向右反转并且我得到正确的地理坐标,那么,我做错了什么?

我可以绘制Polygons, Lines, Markers,Labels等,所以我可以通过正确的图层附加到地图,但我不能Canvas在地图上显示一个简单的圆圈。

我提前感谢您的帮助。

4

1 回答 1

1

您的代码示例遗漏了许多重要部分。你如何使用你的画布,如何将它添加到布局中以及它与 MapView 的关系?什么叫circle.draw?

Nutiteq wiki 有GPS 圆圈示例,你检查过这个吗?这会将 Polygon 对象添加到地图中,并使用地图坐标对其进行操作,这样您就不需要了解任何有关屏幕坐标的信息,所有这些都由 Nutiteq Maps SDK 处理。

于 2014-08-11T16:17:11.287 回答