【问题标题】:Taking multiple coordinates from a PostgreSQL DB and display a polygon从 PostgreSQL 数据库中获取多个坐标并显示一个多边形
【发布时间】:2014-12-09 08:57:26
【问题描述】:

我在理解 ResultSets 时遇到了一些麻烦,所以我问你。我将简要解释我的担忧。

我有一张像这样的表

id(serial) | border(geometry)
    1          latlnggeom1
    2          latlnggeom2
    3          latlnggeom3

我想从该表中获取坐标并将它们显示在 PolygonOptions 中,通常我会这样做:

PolygonOptions p = new PolygonOptions().add(new LatLng(lat1, lng1), new LatLng(lat2, lng2), new LatLng(lat3, lng3));

当坐标不在数据库中,但有 175 个坐标时,我不知道如何从数据库中获取坐标时,我想我不需要在 add() 方法中添加 175 个条目.到目前为止,我的代码如下所示:

prepst = dbops.connect(DB_URL, DB_USER, DB_PW)
                        .prepareStatement(sql);
                rs = prepst.executeQuery();
                while (rs.next()) {
                    int id = rs.getInt("id");
                    double x = rs.getDouble("x");
                    double y = rs.getDouble("y");
                    Log.e(Borders.class.getName(), id + " " + x + " " + y);
                    PolygonOptions rectOptions = new PolygonOptions()
                            .add(new LatLng(x, y)).strokeWidth((float) 1.5)
                            .fillColor(color);
                    map.addPolygon(rectOptions);
                }

Log.e 正确显示值:

1 44.371002 23.739099
2 44.365234 23.749742
...
175 44.370394 23.738563

但我坚持将值添加到 PolygonOptions 并进一步将多边形添加到地图中。提前谢谢你。

【问题讨论】:

    标签: java android sql postgresql polygon


    【解决方案1】:

    我确实解决了这个问题,我使用了 LatLng 的 ArrayList,它工作得很好。下面我把代码贴出来,给以后遇到这个问题的人解释一下。

        //initialize an ArrayList of LatLng type.
        ArrayList<LatLng> border = new ArrayList<LatLng>();
    
        if (dbops.connect(DB_URL, DB_USER, DB_PW).isValid(1)) {
            dbops.disconnect();
            prepst = dbops.connect(DB_URL, DB_USER, DB_PW)
                    .prepareStatement(sql);
            rs = prepst.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("id");
                double x = rs.getDouble("x");
                double y = rs.getDouble("y");
                Log.e(Borders.class.getName(), id + " " + x + " " + y);
                LatLng ll = new LatLng(x, y); //Create a LatLng object to store the x,y doubles inside the array.
                border.add(ll); //add each x,y taken from the database into your ArrayList.
    
            }
            Log.e(Borders.class.getName(), "" + border); //check and make sure your arraylist was created succesfully.
    
            PolygonOptions rectOptions = new PolygonOptions().addAll(border)
                    .strokeWidth((float) 1.5).fillColor(color); //take each item from the arraylist and add it to your polygon
            map.addPolygon(rectOptions);
            Log.e(Borders.class.getName(), "polygonadded");
            dbops.disconnect();
        }
    

    所以你基本上要做的是初始化一个 LatLng 类型的 ArrayList,从数据库中获取数据,然后创建一个 LatLng 对象来存储从数据库中获取的值。之后,将 LatLng 对象存储在 ArrayList 中,最后将 ArrayList 添加到 PolygonOptions。请注意,我将方法 add() 更改为 addAll();

    【讨论】:

      猜你喜欢
      • 2021-12-05
      • 1970-01-01
      • 2015-06-02
      • 1970-01-01
      • 2015-08-08
      • 1970-01-01
      • 2016-03-26
      • 2013-08-24
      • 2015-08-22
      相关资源
      最近更新 更多