如何使用Java中的Google S2库创建多边形

时间:2017-10-26 10:16:16

标签: java android python google-maps google-maps-api-3

我可以使用Google S2 Library创建矩形,但无法创建多边形。我尝试过不同的方式,但无法解决这个问题。

分享我的代码和输出:

 Double point1_lat= 12.9751;
    Double point1_long = 77.5599;
    Double point2_lat= 12.9952;
    Double point2_long = 77.5954;
    Double point3_lat=  12.9741;
    Double point3_long = 77.6246;
    Double point4_lat= 12.9495;
    Double point4_long = 77.59336;
    Double point5_lat= 12.9482;
    Double point5_long = 77.5630;

    S2Point point1 = new S2Point(point1_lat, point1_long, 0);
    S2Point point2 = new S2Point(point2_lat, point2_long, 0);
    S2Point point3 = new S2Point(point3_lat, point3_long, 0);
    S2Point point4 = new S2Point(point4_lat, point4_long, 0);
    S2Point point5 = new S2Point(point5_lat, point5_long, 0);

    S2PolygonBuilder polygonBuilder = new S2PolygonBuilder();

    polygonBuilder.addEdge(point1, point2);
    polygonBuilder.addEdge(point2, point3);
    polygonBuilder.addEdge(point3, point4);
    polygonBuilder.addEdge(point4, point5);
    polygonBuilder.addEdge(point5, point1);

    S2Polygon polygon = polygonBuilder.assemblePolygon();

    System.out.println("***************  debug point 1 ****************" + polygon.toString());

    S2RegionCoverer coverer = new S2RegionCoverer();
    coverer.setMinLevel(5);
    coverer.setMaxLevel(10);
   coverer.setMaxCells(100);

    System.out.println("***************  debug point 2 ****************");


    S2CellUnion union = coverer.getCovering(polygon);

    System.out.println("***************  debug point 3 ****************");


    System.out.println("cells inside the region : "+union.cellIds().size());

输出结果为:

 ***************  debug point 1 ****************Polygon: (1) loops:
   loop <
   (0.0, 80.52526814804811)
   (0.0, 80.52256106366895)
   (0.0, 80.50285721964708)
   (0.0, 80.4926857166327)
   (0.0, 80.51134710630562)
    >

   ***************  debug point 2 ****************
   finally checking for loop end2047
   ***************  debug point 3 ****************
   cells inside the region : 6

你可以在外面看到我只能创建6个单元格,但它应该超过6个单元格,因为整个区域必须只填充单元格。

感谢您的时间,如果您可以建议我如何做到这一点。

谢谢!!!!

1 个答案:

答案 0 :(得分:0)

图书馆拥有自己的内部Lat长表示。你制作的形状覆盖的区域与你想象的完全不同。如果你返回它返回的6个单元格的级别,我怀疑你会发现它们都是非常高的级别(0或1或2)。

您需要使用S2latlong.FromDegreees(y,x)创建一个S2latlong对象然后您可以在刚刚创建的S2LatLng对象上调用.ToPoint()。

其余部分你是对的。使用这些点来形成边和那些边以构建多边形。一旦你进行了latlong转换,它应该适合你。

S2Point p = S2LatLng.FromDegrees(y, x).ToPoint();