我尝试绘制一个蓝色半透明圆圈,以显示Nutiteq's Map
上的实际GPS位置,但它没有显示圆圈。
我想要显示这样的东西
我的代码如下
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(...)
方法向右反转,我得到了正确的地理坐标,所以,我做错了什么? < / p>
我可以绘制Polygons
,Lines
,Markers
,Labels
等等,以便我可以通过右图层附加到地图但我无法显示简单Canvas
在地图上圈出来。
我事先感谢你的帮助。
答案 0 :(得分:2)
您的代码示例错过了许多重要部分。如何使用Canvas,如何将其添加到Layout以及它与MapView的关系?什么叫circle.draw?
Nutiteq维基有GPS circle sample,你检查了吗?这会将Polygon对象添加到地图中,并使用地图坐标对其进行操作,这样您就不需要了解任何有关屏幕坐标的信息,所有这些都由Nutiteq Maps SDK处理。