动机:
我正在创建一个执行以下操作的android应用程序。
问题:
我使算法起作用。但是转换数据类型存在问题。
输入是一个Point数组(Point是一个自定义类)。
geoPoints = new Point[7];
geoPoints[0] = new Point(new GeoPoint(8.180992, 77.336551));
但是绘制多边形的osmdroid函数需要使用List
List<GeoPoint> gPoints;
polygon.setPoints(gPoints);
我设法在Point()构造函数上进行了转换,如下所示
public Point(GeoPoint geoPoint) {
this.x = geoPoint.getLatitude();
this.y = geoPoint.getLongitude();
}
但是我不知道如何计算返回类型。
那么,当我运行代码时。我收到以下错误。
error: incompatible types: inference variable T has incompatible bounds
equality constraints: GeoPoint
lower bounds: Point
where T is a type-variable:
T extends Object declared in method <T>asList(T...)
呼叫功能:
private void callingFunction() {
geoPoints = new Point[7];
geoPoints[0] = new Point(new GeoPoint(8.180992, 77.336551));
geoPoints[1] = new Point(new GeoPoint(8.183966, 77.340353));
geoPoints[2] = new Point(new GeoPoint(8.179836, 77.336105));
geoPoints[3] = new Point(new GeoPoint(8.178744, 77.339179));
geoPoints[4] = new Point(new GeoPoint(8.182155, 77.341925));
geoPoints[5] = new Point(new GeoPoint(8.181655, 77.339318));
geoPoints[6] = new Point(new GeoPoint(8.182155, 77.341925));
polygon = new Polygon(); //see note below
polygon.setFillColor(Color.parseColor("#80FFE082"));
polygon.setStrokeColor(Color.parseColor("#FFD54F"));
polygon.setStrokeWidth(5f);
ConvexHull cx = new ConvexHull(geoPoints);
Point C1[]= cx.getConvexHull();
polygon.setPoints(Arrays.asList(C1));
map.getOverlayManager().add(polygon);
map.invalidate();
}
注意:
不能仅使用列表或仅使用数组,因为
polygon.setPoints(geoPoints) //needs List<GeoPoint> in callingFunction
Arrays.sort(points); //requires array of Points in ConvexHull
所提供的一切。我怎么解决这个问题?任何形式的帮助将非常有用。并预先感谢。
答案 0 :(得分:1)
我不确定,如果我说对了,但基本上您的问题归结为以下事实:您有一个Point
对象数组,但需要一个GeoPoint
对象列表, 对? Point
和GeoPoint
之间没有关系,因此一个不会扩展另一个。
那么为什么不在绘制多边形之前将Point
对象转换回GeoPoint
对象呢?
您可以在流中映射类型:
ConvexHull cx = new ConvexHull(geoPoints);
Point C1[] = cx.getConvexHull();
polygon.setPoints(Arrays.stream(C1).map(p -> new GeoPoint(p.getX(), p.getY())).collect(Collectors.toList());
或使用for
循环:
ConvexHull cx = new ConvexHull(geoPoints);
Point C1[] = cx.getConvexHull();
List<GeoPoint> points = new ArrayList<GeoPoint>(C1.length);
for (int i = 0; i < C1.length; ++i) {
points.add(new GeoPoint(C1[i].getX(), C1[i].getY()));
}
polygon.setPoints(points);