我正在尝试将ListArray coords
中的值添加到ListArray linhasCoords
,但linhasCoords
并未获取所有值。
我的观点是,对于每个ID,保存所有坐标Lat和Long。
我的代码:http://pastebin.com/P2k82wQ3
public HashMap<Integer, ArrayList<LatLong>> load_RoadAxis() {
HashMap<Integer,ArrayList<LatLong>> linhasCoords = new HashMap<Integer, ArrayList<LatLong>>();
ArrayList<LatLong> coords = new ArrayList<LatLong>();
int idAtual = 0;
int idAnterior = 1;
String query = "SELECT y, x, linha2 FROM trechos_pontos WHERE linha2 <5 ORDER BY linha2, path ";
try {
Stmt stmt = db.prepare(query);
while (stmt.step()) {
Double x = stmt.column_double(0);
Double y = stmt.column_double(1);
//Log.d("TRECHOS", "idAtual: " + idAtual + "X: " + x.toString() + " Y: " + y.toString());
idAtual = stmt.column_int(2);
Log.d("TRECHOS", "idAtual: " + idAtual + " idAnterior: " + idAnterior);
if (idAtual == idAnterior) {
coords.add(new LatLong(x,y));
Log.d("TRECHOS", " Coords: " + coords);
} else {
linhasCoords.put(idAnterior,coords);
idAnterior = idAtual;
coords.clear();
coords.add(new LatLong(x,y));
Log.d("TRECHOS", " LinhasCoords: " + linhasCoords);
}
}
stmt.close();
} catch (Exception e) {
e.printStackTrace();
Log.d("TRECHOS", "A consulta dos trechos falhou");
}
//Log.d("TRECHOS", linhasCoords.toString());
return linhasCoords;
}
我的日志的一部分:http://pastebin.com/pDUat9CL
D/TRECHOS(21428): idAtual: 1 idAnterior: 1
D/TRECHOS(21428): Coords: [latitude=43.5922273767277, longitude=1.44029380540736]
D/TRECHOS(21428): idAtual: 1 idAnterior: 1
D/TRECHOS(21428): Coords: [latitude=43.5922273767277, longitude=1.44029380540736, latitude=43.5922136050099, longitude=1.44047259717341]
D/TRECHOS(21428): idAtual: 2 idAnterior: 1
D/TRECHOS(21428): LinhasCoords: {1=[latitude=43.5925479521396, longitude=1.4365973553446]}
D/TRECHOS(21428): idAtual: 2 idAnterior: 2
D/TRECHOS(21428): Coords: [latitude=43.5925479521396, longitude=1.4365973553446, latitude=43.5926291677532, longitude=1.43564264638214]
D/TRECHOS(21428): idAtual: 2 idAnterior: 2
D/TRECHOS(21428): Coords: [latitude=43.5925479521396, longitude=1.4365973553446, latitude=43.5926291677532, longitude=1.43564264638214, latitude=43.5926625330203, longitude=1.43526523586817]
D/TRECHOS(21428): idAtual: 2 idAnterior: 2
D/TRECHOS(21428): Coords: [latitude=43.5925479521396, longitude=1.4365973553446, latitude=43.5926291677532, longitude=1.43564264638214, latitude=43.5926625330203, longitude=1.43526523586817, latitude=43.5926642101564, longitude=1.43523990190003]
D/TRECHOS(21428): idAtual: 2 idAnterior: 2
D/TRECHOS(21428): Coords: [latitude=43.5925479521396, longitude=1.4365973553446, latitude=43.5926291677532, longitude=1.43564264638214, latitude=43.5926625330203, longitude=1.43526523586817, latitude=43.5926642101564, longitude=1.43523990190003, latitude=43.5922914378148, longitude=1.43956435379756]
D/TRECHOS(21428): idAtual: 2 idAnterior: 2
D/TRECHOS(21428): Coords: [latitude=43.5925479521396, longitude=1.4365973553446, latitude=43.5926291677532, longitude=1.43564264638214, latitude=43.5926625330203, longitude=1.43526523586817, latitude=43.5926642101564, longitude=1.43523990190003, latitude=43.5922914378148, longitude=1.43956435379756, latitude=43.5923036302806, longitude=1.43941791555995]
D/TRECHOS(21428): idAtual: 2 idAnterior: 2
D/TRECHOS(21428): Coords: [latitude=43.5925479521396, longitude=1.4365973553446, latitude=43.5926291677532, longitude=1.43564264638214, latitude=43.5926625330203, longitude=1.43526523586817, latitude=43.5926642101564, longitude=1.43523990190003, latitude=43.5922914378148, longitude=1.43956435379756, latitude=43.5923036302806, longitude=1.43941791555995, latitude=43.5923244338855, longitude=1.43916026189481]
D/TRECHOS(21428): idAtual: 2 idAnterior: 2
D/TRECHOS(21428): Coords: [latitude=43.5925479521396, longitude=1.4365973553446, latitude=43.5926291677532, longitude=1.43564264638214, latitude=43.5926625330203, longitude=1.43526523586817, latitude=43.5926642101564, longitude=1.43523990190003, latitude=43.5922914378148, longitude=1.43956435379756, latitude=43.5923036302806, longitude=1.43941791555995, latitude=43.5923244338855, longitude=1.43916026189481, latitude=43.5923490976508, longitude=1.43888705045567]
D/TRECHOS(21428): idAtual: 2 idAnterior: 2
D/TRECHOS(21428): Coords: [latitude=43.5925479521396, longitude=1.4365973553446, latitude=43.5926291677532, longitude=1.43564264638214, latitude=43.5926625330203, longitude=1.43526523586817, latitude=43.5926642101564, longitude=1.43523990190003, latitude=43.5922914378148, longitude=1.43956435379756, latitude=43.5923036302806, longitude=1.43941791555995, latitude=43.5923244338855, longitude=1.43916026189481, latitude=43.5923490976508, longitude=1.43888705045567, latitude=43.592377137392, longitude=1.4385645839831]
D/TRECHOS(21428): idAtual: 2 idAnterior: 2
D/TRECHOS(21428): Coords: [latitude=43.5925479521396, longitude=1.4365973553446, latitude=43.5926291677532, longitude=1.43564264638214, latitude=43.5926625330203, longitude=1.43526523586817, latitude=43.5926642101564, longitude=1.43523990190003, latitude=43.5922914378148, longitude=1.43956435379756, latitude=43.5923036302806, longitude=1.43941791555995, latitude=43.5923244338855, longitude=1.43916026189481, latitude=43.5923490976508, longitude=1.43888705045567, latitude=43.592377137392, longitude=1.4385645839831, latitude=43.5924023567609, longitude=1.43825904413027]
D/TRECHOS(21428): idAtual: 2 idAnterior: 2
D/TRECHOS(21428): Coords: [latitude=43.5925479521396, longitude=1.4365973553446, latitude=43.5926291677532, longitude=1.43564264638214, latitude=43.5926625330203, longitude=1.43526523586817, latitude=43.5926642101564, longitude=1.43523990190003, latitude=43.5922914378148, longitude=1.43956435379756, latitude=43.5923036302806, longitude=1.43941791555995, latitude=43.5923244338855, longitude=1.43916026189481, latitude=43.5923490976508, longitude=1.43888705045567, latitude=43.592377137392, longitude=1.4385645839831, latitude=43.5924023567609, longitude=1.43825904413027, latitude=43.5924486368068, longitude=1.43771410203014]
D/TRECHOS(21428): idAtual: 2 idAnterior: 2
D/TRECHOS(21428): Coords: [latitude=43.5925479521396, longitude=1.4365973553446, latitude=43.5926291677532, longitude=1.43564264638214, latitude=43.5926625330203, longitude=1.43526523586817, latitude=43.5926642101564, longitude=1.43523990190003, latitude=43.5922914378148, longitude=1.43956435379756, latitude=43.5923036302806, longitude=1.43941791555995, latitude=43.5923244338855, longitude=1.43916026189481, latitude=43.5923490976508, longitude=1.43888705045567, latitude=43.592377137392, longitude=1.4385645839831, latitude=43.5924023567609, longitude=1.43825904413027, latitude=43.5924486368068, longitude=1.43771410203014, latitude=43.5924671434473, longitude=1.4375098967783]
D/TRECHOS(21428): idAtual: 2 idAnterior: 2
D/TRECHOS(21428): Coords: [latitude=43.5925479521396, longitude=1.4365973553446, latitude=43.5926291677532, longitude=1.43564264638214, latitude=43.5926625330203, longitude=1.43526523586817, latitude=43.5926642101564, longitude=1.43523990190003, latitude=43.5922914378148, longitude=1.43956435379756, latitude=43.5923036302806, longitude=1.43941791555995, latitude=43.5923244338855, longitude=1.43916026189481, latitude=43.5923490976508, longitude=1.43888705045567, latitude=43.592377137392, longitude=1.4385645839831, latitude=43.5924023567609, longitude=1.43825904413027, latitude=43.5924486368068, longitude=1.43771410203014, latitude=43.5924671434473, longitude=1.4375098967783, latitude=43.5925346838359, longitude=1.43673959999082]
D/TRECHOS(21428): idAtual: 3 idAnterior: 2
D/TRECHOS(21428): LinhasCoords: {1=[latitude=43.5921680888747, longitude=1.44100065185853], 2=[latitude=43.5921680888747, longitude=1.44100065185853]}
D/TRECHOS(21428): idAtual: 3 idAnterior: 3
D/TRECHOS(21428): Coords: [latitude=43.5921680888747, longitude=1.44100065185853, latitude=43.592147899421, longitude=1.4412301885968]
D/TRECHOS(21428): idAtual: 3 idAnterior: 3
D/TRECHOS(21428): Coords: [latitude=43.5921680888747, longitude=1.44100065185853, latitude=43.592147899421, longitude=1.4412301885968, latitude=43.5921435012598, longitude=1.44127947779876]
D/TRECHOS(21428): idAtual: 3 idAnterior: 3
D/TRECHOS(21428): Coords: [latitude=43.5921680888747, longitude=1.44100065185853, latitude=43.592147899421, longitude=1.4412301885968, latitude=43.5921435012598, longitude=1.44127947779876, latitude=43.5921339259824, longitude=1.44139493077248]
D/TRECHOS(21428): idAtual: 4 idAnterior: 3
D/TRECHOS(21428): LinhasCoords: {1=[latitude=43.5922136050099, longitude=1.44047259717341], 2=[latitude=43.5922136050099, longitude=1.44047259717341], 3=[latitude=43.5922136050099, longitude=1.44047259717341]}
D/TRECHOS(21428): idAtual: 4 idAnterior: 4
D/TRECHOS(21428): Coords: [latitude=43.5922136050099, longitude=1.44047259717341, latitude=43.5922090466017, longitude=1.44051065409724]
D/TRECHOS(21428): idAtual: 4 idAnterior: 4
D/TRECHOS(21428): Coords: [latitude=43.5922136050099, longitude=1.44047259717341, latitude=43.5922090466017, longitude=1.44051065409724, latitude=43.5922038095384, longitude=1.44057260588265]
D/TRECHOS(21428): idAtual: 4 idAnterior: 4
D/TRECHOS(21428): Coords: [latitude=43.5922136050099, longitude=1.44047259717341, latitude=43.5922090466017, longitude=1.44051065409724, latitude=43.5922038095384, longitude=1.44057260588265, latitude=43.592196812945, longitude=1.4406542751152]
D/TRECHOS(21428): idAtual: 4 idAnterior: 4
D/TRECHOS(21428): Coords: [latitude=43.5922136050099, longitude=1.44047259717341, latitude=43.5922090466017, longitude=1.44051065409724, latitude=43.5922038095384, longitude=1.44057260588265, latitude=43.592196812945, longitude=1.4406542751152, latitude=43.5921951761103, longitude=1.44068241680208]
您可以看到linhasCoords
在generated
之前没有所有值及其coords
,我也不知道为什么。
答案 0 :(得分:0)
if (idAtual == idAnterior) {
coords.add(new LatLong(x,y));
Log.d("TRECHOS", " Coords: " + coords);
} else {
linhasCoords.put(idAnterior,coords);
idAnterior = idAtual;
coords.clear();
coords.add(new LatLong(x,y));
Log.d("TRECHOS", " LinhasCoords: " + linhasCoords);
}
因此,如果idAtual等于idAnterior,它将不会被添加到linhasCoords,只会添加到coords。 替换为:
if (idAtual == idAnterior) {
linhasCoords.put(idAnterior,coords);
coords.add(new LatLong(x,y));
Log.d("TRECHOS", " Coords: " + coords);
} else {
linhasCoords.put(idAnterior,coords);
idAnterior = idAtual;
coords.clear();
coords.add(new LatLong(x,y));
Log.d("TRECHOS", " LinhasCoords: " + linhasCoords);
}