简体   繁体   中英

Taking multiple coordinates from a PostgreSQL DB and display a polygon

I have some trouble understanding ResultSets so I'm asking you. I will be brief in explaining my concern.

I have a table that looks like this

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

And I want to take the coordinates from that table and display them in a PolygonOptions, normally I would do:

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

when the coordinates are not in a database, but there are 175 coordinates and I have no idea how to do that when taking the coordinates from a database, I guess I am not needed to add 175 entries in that add() method. My code so far looks like:

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);
                }

The Log.e displays the values correctly:

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

But I am stuck in adding the values to the PolygonOptions and furtherly add the polygon to the map. Thank you in advance.

I did solve this, I used an ArrayList of LatLng and it works just fine. I will post the code and explain it below for whoever will encounter this problem in the future.

    //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();
    }

So what you basically do, is you initialize an ArrayList of type LatLng, you take your data from the database and you create a LatLng object to store your values taken from the database. Afterwards you store your LatLng object in your ArrayList and in the end you add your ArrayList to your PolygonOptions. Note that I changed the method add() with addAll();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM