![](/img/trans.png)
[英]Taking a picture and markers from a DB and display them in a marker custom infowindow
[英]Taking multiple coordinates from a PostgreSQL DB and display a polygon
我在理解ResultSet時遇到了一些麻煩,所以我問你。 我將簡短地解釋我的關注。
我有一張看起來像這樣的桌子
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中,並進一步將多邊形添加到地圖中。 先感謝您。
我確實解決了這個問題,我使用了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中。 注意,我用addAll()更改了方法add()。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.