简体   繁体   中英

android sqlite cursor to maps adapter

I've got this section of code within my DataSource class to make a List of the lat and long values stored within my sqlite database. I'm trying to take this list and use it to add circles to the maps api v2 using mMap.addCircle by setting the circleOptions. How do I connect the cursor list to create circles?

 CircleOptions circleOptions = new CircleOptions()
        .center(new LatLng(location.getLatitude(), location.getLongitude())); 

Datasource List:

public List<Position> getAllPositions() {
    List<Position> position = new ArrayList<Position>();

    Cursor cursor = database.query(MySQLiteHelper.TABLE_NAME,
        allColumns, null, null, null, null, null);

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) { 

        String positions = cursor.getString(1);
    double latitude = cursor.getColumnIndex("lat");
    double longitude = cursor.getColumnIndex("long");

cursor.moveToNext();
    }
    // make sure to close the cursor
    cursor.close();
    return positions;
  }

I don't think that code would work correctly, first because in getAllPositions function you are returning something called "positions" which is not visible from there, and even if you returned "position", which is the position array, it seems to be empty...

You could use LatLng class as follows

public List<LatLng> getAllPositions() {
List<LatLng> positionList = new ArrayList<LatLng>();

Cursor cursor = database.query(MySQLiteHelper.TABLE_NAME,
    allColumns, null, null, null, null, null);

cursor.moveToFirst();
while (!cursor.isAfterLast()) { 

    String positions = cursor.getString(1);
    double latitude = cursor.getColumnIndex("lat");
    double longitude = cursor.getColumnIndex("long");
    LatLng newLatLng = new LatLng(latitude, longitude);
    positionList.add(newLatLng);
    cursor.moveToNext();
}
// make sure to close the cursor
cursor.close();
return positionList;
}

And then:

List<LatLng> positionToDraw =  getAllPositions();
for(int i=0;i<positiontoDraw.size();i++){
     CircleOptions circleOptions = new CircleOptions().center(positionToDraw.get(i)); 
     // [...]
}

Let me know if it helped.

Regards

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