如何在Android地图v2中计算复杂多边形的轮廓 [英] How to calculate outline of complex polygon in Android maps v2

查看:137
本文介绍了如何在Android地图v2中计算复杂多边形的轮廓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Android maps v2中填充多边形的中心,并且在多边形复杂并且线条相互交叉时遇到问题。用户可以用手指在地图上绘制,然后使用地图投影将我的点转换为LatLng。



我需要填充中心,即使它是用交叉线条绘制的。



我的代码绘制如下所示:

  PolygonOptions rectOptions = new PolygonOptions(); 
rectOptions.strokeColor(getResources()。getColor(R.color.blue));
rectOptions.fillColor(getResources()。getColor(R.color.blue_map_fill));
rectOptions.strokeWidth(4);
rectOptions.addAll(latLngs);
mMap.addPolygon(rectOptions);

屏幕截图当我画着一个星形线时:

屏幕截图仅以轮廓绘制明星:



是有一种方法来计算什么LatLngs组成大纲或有不同的解决方案?编辑:我正在处理的应用程序的iOS版本处理完美。他们只是将所有点加入到一个多边形中,然后谷歌地图将其计算出来。在这一点上,我相信这是一个缺乏Android谷歌地图的bug /功能。



编辑:错误报告: https://code.google.com/p/gmaps-api-issues/issues/detail?can=2&start=0& ; num = 100& q =& colspec = ID%20Type%20Status%20Introduced%20Fixed%20Summary%20Stars%20ApiType%20Internal& groupby =& sort =& id = 6255

$ b

解决方案

我知道这已经过去了一年,但这是我的解决方案。
我使用 JTS库,我认为您可以使用此客户端以创建周围的多边形。



此方法创建一个新列表 LatLng 对象,您可以使用它绘制输入的周围多边形。

  private void createSurroundingPolygon(List< LatLng> polygonPath){
List< Coordinate> coordinates = new ArrayList<>(); (LatLng latLng:polygonPath){
coordinates.add(new Coordinate(latLng.longitude,latLng.latitude));

}

GeometryFactory factory = new GeometryFactory();
Geometry lineString = factory.createLineString(coordinates.toArray(new Coordinate [coordinates.size()]));
Polygon polygon =(Polygon)BufferOp.bufferOp(lineString,0.0001);

Coordinate [] coordinatesSurroundingPolygon = polygon.getExteriorRing()。getCoordinates();
列表< LatLng> surroundPolygon = new ArrayList<>();
for(int i = 0; i< coordinatesSurroundingPolygon.length; i ++){
surroundPolygon.add(new LatLng(coordinatesSurroundingPolygon [i] .y,coordinatesSurroundingPolygon [i] .x));
}
drawPolygon(surroundingPolygon);
}

首先创建一个新的Coordindates列表。它们用于创建JTS几何体,即LineString。您不能直接从坐标列表创建LinearRing或Polygon,因为您不知道它是否是有效的(闭合多边形没有交点)。当你用给定的几何图形缓冲距离时,你会得到一个多边形,在我的情况下是0.0001。距离是额外的空间,它被添加到您的原始多边形之外。



最后用方法 Polygon.getExtgeriorRing()得到没有任何交叉和交叉线的轮廓多边形。


I need to fill the center of a polygon in Android maps v2 and am running into problems when the polygon is complex and has lines that cross each other. The user is able to draw on the map with their finger and then I use a map projection to convert my points to LatLng.

I need to fill the center even if it is drawn with lines that cross.

My code to draw is as follows:

    PolygonOptions rectOptions = new PolygonOptions();
    rectOptions.strokeColor(getResources().getColor(R.color.blue));
    rectOptions.fillColor(getResources().getColor(R.color.blue_map_fill));
    rectOptions.strokeWidth(4);
    rectOptions.addAll(latLngs);
    mMap.addPolygon(rectOptions);

Screenshot when I draw a star with the lines crossing:

Screenshot when I draw the star with only the outline:

Is there a way to calculate what LatLngs make up the outline or is there a different solution?

EDIT: The iOS version of the app I'm working on handles this perfectly..they just add all the points to a polygon and Google Maps figures it out. At this point I believe this is a bug/feature that is lacking from Android Google Maps.

EDIT: Bug report: https://code.google.com/p/gmaps-api-issues/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Type%20Status%20Introduced%20Fixed%20Summary%20Stars%20ApiType%20Internal&groupby=&sort=&id=6255

解决方案

I know it's already a year gone but here is my solution. I use the JTS library, I think you can use a client of this library as well, to create the surrounding polygon.

This method creates a new list of LatLng objects which you can use to draw the surrounding polygon of your input.

private void createSurroundingPolygon(List<LatLng> polygonPath) {
    List<Coordinate> coordinates = new ArrayList<>();
    for (LatLng latLng : polygonPath) {
        coordinates.add(new Coordinate(latLng.longitude, latLng.latitude));
    }

    GeometryFactory factory = new GeometryFactory();
    Geometry lineString = factory.createLineString(coordinates.toArray(new Coordinate[coordinates.size()]));
    Polygon polygon = (Polygon) BufferOp.bufferOp(lineString, 0.0001);

    Coordinate[] coordinatesSurroundingPolygon = polygon.getExteriorRing().getCoordinates();
    List<LatLng> surroundingPolygon = new ArrayList<>();
    for (int i = 0; i < coordinatesSurroundingPolygon.length; i++) {
        surroundingPolygon.add(new LatLng(coordinatesSurroundingPolygon[i].y, coordinatesSurroundingPolygon[i].x));
    }
    drawPolygon(surroundingPolygon);
}

First it creates a new list of Coordindates. They are used to create a JTS Geometry, i.e. a LineString. You can't create a LinearRing or a Polygon directly from your coordinates list because you don't know if it is a valid (closed polygon without intersections) one. You get a Polygon when you buffer the given Geometry with a distance, in my case 0.0001. The distance is additional space which is added outside to your original polygon.

Finally with the method Polygon.getExtgeriorRing() you get the outline polygon without any intersections and crossing lines.

这篇关于如何在Android地图v2中计算复杂多边形的轮廓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆