gpt4 book ai didi

Android map 自定义覆盖不可见

转载 作者:行者123 更新时间:2023-11-29 14:06:50 26 4
gpt4 key购买 nike

我正在尝试在 android 的 mapview 上显示一个多边形。我创建了一个自定义覆盖类(多边形)并重写了 draw 方法。将多边形实例添加到 map View 的叠加列表后,“应该”显示一个多边形。但是显示 map 时,找不到叠加层。我错过了什么?这是创建多边形的主要 map Activity :

public class GPSLocator extends MapActivity {
MapView mapView;
Polygon polygon;

@Override
protected boolean isRouteDisplayed() {
return false;
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);

ArrayList<GeoPoint> points = new ArrayList<GeoPoint>();
points.add(new GeoPoint((int)(-86.63283601665323*1e6), (int)(34.6857467079488*1e6)));
points.add(new GeoPoint((int)(-86.63172145427183*1e6), (int)(34.68572865382659*1e6)));
points.add(new GeoPoint((int)(-86.63172141228351*1e6), (int)(34.68613493108094*1e6)));
points.add(new GeoPoint((int)(-86.63288804303616*1e6), (int)(34.68611093719812*1e6)));
polygon = new Polygon(points);

mapView.getOverlays().clear();
mapView.getOverlays().add(polygon);
mapView.invalidate();
}

这是自定义叠加多边形:

public class Polygon extends Overlay {

ArrayList<GeoPoint> geoPoints;

public Polygon(ArrayList<GeoPoint> points)
{
geoPoints = points;
}

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow)
{
//Set the color and style
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.FILL_AND_STROKE);

//Create path and add points
Path path = new Path();
Point firstPoint = new Point();
mapView.getProjection().toPixels(geoPoints.get(0), firstPoint);
path.moveTo(firstPoint.x, firstPoint.y);

for(int i = 1; i < geoPoints.size(); ++i)
{
Point nextPoint = new Point();
mapView.getProjection().toPixels(geoPoints.get(i), nextPoint);
path.lineTo(nextPoint.x, nextPoint.y);
}

//Close polygon
path.lineTo(firstPoint.x, firstPoint.y);
path.setLastPoint(firstPoint.x, firstPoint.y);
canvas.drawPath(path, paint);
super.draw(canvas, mapView, shadow);

}
}

最佳答案

你必须使用 path.moveTo(pixelPoint.x, pixelPoint.y);在你的绘图循环中。否则,您将绘制星形而不是多边形。这是我用于类似任务的(工作)代码:

public void draw(Canvas canvas, MapView mapv, boolean shadow) {
super.draw(canvas, mapv, shadow);
Log.i("BikeComputer", "MyOverlay draw");
mPaint = new Paint();
mPaint.setDither(true);
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(5);

Point p1 = new Point();
path = new Path();

GeoPoint point = geoPoints.get(0);
mapv.getProjection().toPixels(point, p1);
path.moveTo(p1.x, p1.y);

Point pixelPoint = new Point();
Iterator<GeoPoint> i = geoPoints.iterator();
while (i.hasNext()) {

GeoPoint trackPoint = i.next();
mapv.getProjection().toPixels(trackPoint, pixelPoint);
path.lineTo(pixelPoint.x, pixelPoint.y);
path.moveTo(pixelPoint.x, pixelPoint.y);
}
// draw track
canvas.drawPath(path, mPaint);

// start point
mapv.getProjection().toPixels(geoPoints.get(0), pixelPoint);
canvas.drawBitmap(startFlag, pixelPoint.x, pixelPoint.y - 32, null);

// finish point

mapv.getProjection().toPixels(geoPoints.get(geoPoints.size() - 1),
pixelPoint);
canvas.drawBitmap(finishFlag, pixelPoint.x, pixelPoint.y - 32, null);
}

}

这个类是如何实例化的:

        mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);

mapOverlays = mapView.getOverlays();
mapView.getProjection();
mapOverlays.add(new MyOverlay());

关于Android map 自定义覆盖不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6446467/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com