【问题标题】:SQlite & add a multiply markers on Google MapSQlite & 在谷歌地图上添加一个乘法标记
【发布时间】:2016-01-23 19:24:05
【问题描述】:

我想在地图上绘制自定义标记。如何组织多个标记的显示。

public class DatabaseHelper extends SQLiteOpenHelper {

static final String TABLE = "geotable";

public DatabaseHelper(Context context) {
    super(context, "myDB", null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table geotable ("
            + "id integer primary key autoincrement,"
            + "image text,"
            + "lng text,"
            + "lat text" + ");");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS "+TABLE);
    onCreate(db);
}

在第一堂课中,我在 db 中创建表。然后把信息放在这个表中

case R.id.saveToBD:

            cv.put("image", tex);
            cv.put("lng", lng);
            cv.put("lat", ltd); 
            break;

然后我需要显示从 DB 到映射的所有标记。但我在地图上只看到一个(最后一个)标记。如何组织循环?

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

DatabaseHelper sqlHelper;
SQLiteDatabase db;
Cursor userCursor;

private GoogleMap googleMap;
private String imageTest;
double lngTest, ltdTest;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    sqlHelper = new DatabaseHelper(getApplicationContext());
}

@Override
public void onResume() {
    super.onResume();
    db = sqlHelper.getReadableDatabase();

    userCursor = db.query("geotable", null, null, null, null, null, null);

    if (userCursor.moveToFirst()) {

        int imageColIndex = userCursor.getColumnIndex("image");
        int lngColIndex = userCursor.getColumnIndex("lng");
        int latlColIndex = userCursor.getColumnIndex("lat");

        do {

        imageTest = userCursor.getString(imageColIndex);
        lngTest = userCursor.getDouble(lngColIndex);
        ltdTest = userCursor.getDouble(latlColIndex);

        }
            while (userCursor.moveToNext());
    }

    else
userCursor.close();

}

@Override
public void onDestroy() {
    super.onDestroy();
    db.close();
    userCursor.close();
}

@Override
public void onMapReady(GoogleMap googleMap) {
    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
            new LatLng(ltdTest, lngTest), 16));

    // You can customize the marker image using images bundled with
    // your app, or dynamically generated bitmaps.

        googleMap.addMarker(new MarkerOptions()
                .icon(BitmapDescriptorFactory.fromPath(imageTest))
                .anchor(0.0f, 1.0f) // Anchors the marker on the bottom left
                .position(new LatLng(ltdTest, lngTest)));

}

添加数组列表,

        if (userCursor.moveToFirst()) {

        int imageColIndex = userCursor.getColumnIndex("image");
        int lngColIndex = userCursor.getColumnIndex("lng");
        int latlColIndex = userCursor.getColumnIndex("lat");

        do {

        imageTest = userCursor.getString(imageColIndex);
        lngTest = userCursor.getDouble(lngColIndex);
        ltdTest = userCursor.getDouble(latlColIndex);

            geoList.add(imageTest);
            geoList.add(lngTest);
            geoList.add(ltdTest);

        }
            while (userCursor.moveToNext());
    }

然后用循环显示标记

        for (int i = 0; i < geoList.size(); i=i+3) {

        googleMap.addMarker(new MarkerOptions()
                .icon(BitmapDescriptorFactory.fromPath((String) geoList.get(i)))
                .anchor(0.0f, 1.0f) // Anchors the marker on the bottom left
                .position(new LatLng((Double) geoList.get(i+2), (Double) geoList.get(i+1))));

    }

在地图上只显示最后添加到 DB 标记,我添加了 2-10 个标记,但只看到最后一个。

【问题讨论】:

    标签: java android sqlite google-maps cycle


    【解决方案1】:

    您正在从geotable 中的整数变量中获取数据onResume()

    imageTest = userCursor.getString(imageColIndex);
    lngTest = userCursor.getDouble(lngColIndex);
    ltdTest = userCursor.getDouble(latlColIndex);
    

    然后在onMapReady(GoogleMap googleMap) 中,您使用上述变量的值添加标记,这些变量仅包含您的geotable 的最后一行数据:

    googleMap.addMarker(new MarkerOptions()
        .icon(BitmapDescriptorFactory.fromPath(imageTest))
        .anchor(0.0f, 1.0f) // Anchors the marker on the bottom left
        .position(new LatLng(ltdTest, lngTest)));
    

    您应该使用 ArrayList 从 geotable 获取数据,并在 onMapReady(GoogleMap googleMap) 的循环内添加多个标记。

    【讨论】:

    • 请给我更多关于aloop坐标的信息
    • 我没有更正将信息放入数据库,所有标记都设置为 id = 0 的表;
    猜你喜欢
    • 2015-08-18
    • 2016-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    相关资源
    最近更新 更多