简体   繁体   中英

Android - google map - how show multiple markers on the map?

I want to show multiple markers (may be 100 or more) on google map . I am reading lat and lng from sqlite then I add them on google map, but show me just one marker ?

Do I have to use Thread or AsyncTask ?

Here is my code :

public class StartActivity extends AppCompatActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    public SQLiteDatabase sql;
    public Cursor cursor;
    Context context;
    public static String PACKAGE_NAME;
    ArrayList<LatLng> markerPoints;
    LatLng newLatLng;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        // 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);

        context = getApplicationContext();
        DBS db = new DBS(context);
        PACKAGE_NAME = context.getApplicationContext().getPackageName();
        db.GetPackageName(PACKAGE_NAME);
        db.CreateFile();
        try {
            db.CreateandOpenDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }
        sql = db.openDataBase();

        try {
            cursor = sql.rawQuery("SELECT _Lat,_Lng,_Date,_Time FROM Places where UserCode = 101", null);
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    do {
                        markerPoints = new ArrayList<LatLng>();
                        Structure_DB sdb = new Structure_DB();
                        sdb._Lat = cursor.getString(cursor.getColumnIndex("_Lat"));
                        sdb._Lng = cursor.getString(cursor.getColumnIndex("_Lng"));
                        sdb._Date = cursor.getString(cursor.getColumnIndex("_Date"));
                        sdb._Time = cursor.getString(cursor.getColumnIndex("_Time"));
                        newLatLng = new LatLng(Double.parseDouble(sdb._Lat), Double.parseDouble(sdb._Lng));
                        markerPoints.add(newLatLng);
                    } while (cursor.moveToNext());
                }
            }
        } catch (Exception e) {
        } finally {
            cursor.close();
        }
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        Iterator<LatLng> iterator = markerPoints.iterator();
        while (iterator.hasNext()) {
            MarkerOptions markerOptions = new MarkerOptions()
                    .position(iterator.next());
            mMap.addMarker(markerOptions);
        }
    }
}

You are initializing your markerPoints on each iteration.

Remove markerPoints = new ArrayList<LatLng>(); from the do-while loop and initialize it on the declaration ArrayList<LatLng> markerPoints = new ArrayList<LatLng>();

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