简体   繁体   English

SQlite并在Google Map上添加乘法标记

[英]SQlite & add a multiply markers on Google Map

I want a draw custom markers on map. 我想要在地图上绘制自定义标记。 How to organize the display of many markers. 如何组织许多标记的显示。

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

In first class i'am create table in db. 在第一堂课中,我将在db中创建表。 Then put info in this table 然后在此表中放入信息

case R.id.saveToBD:

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

and then i need a display all markers from DB to map. 然后我需要显示从数据库到地图的所有标记。 But i see only one (last) marker on map. 但是我在地图上只看到一个(最后一个)标记。 How do I organize a cycle ? 如何组织一个周期?

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

}

add ArrayList, 添加ArrayList,

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

Then display markers with cycle 然后显示带有周期的标记

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

    }

On map shows only last added to DB marker, i'am add a 2-10 markers but see only last. 在地图上显示仅最后添加到DB标记,我添加了2-10个标记,但仅看到最后一个。

You are fetching data from your geotable in integer variables in onResume() : 您正在使用onResume()中的整数变量从geotable中获取数据:

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

Then in onMapReady(GoogleMap googleMap) you are adding marker using values of above variables which contains only last row data of your geotable : 然后在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)));

You should use an ArrayList to get data from geotable and add multiple markers inside a loop in onMapReady(GoogleMap googleMap) . 您应该使用ArrayList从geotable获取数据,并在onMapReady(GoogleMap googleMap)的循环内添加多个标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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