1

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);
        }
    }
}
Ha.
  • 3,454
  • 21
  • 24
  • Possible duplicate of [How to show multiple markers on MapFragment in Google Map API v2?](http://stackoverflow.com/questions/13855049/how-to-show-multiple-markers-on-mapfragment-in-google-map-api-v2) – Aman Gupta - ΔMΔN Jul 25 '16 at 08:32

1 Answers1

0

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

antonio
  • 18,044
  • 4
  • 45
  • 61
  • antonio,my point is a lot of when I run my app, app too slow.What can I do? Do I have to use Thread or AsyncTask ...? –  Jul 25 '16 at 08:49
  • 2
    You could (and must) use an `AsyncTask` to query database, but unfortunately marker drawing must be done on the main (UI) thread and is a really heavy operation. The only option to improve speed is trying to reduce the number of markers (map elements) being drawn. For example using marker clustering (https://developers.google.com/maps/documentation/android-api/utility/marker-clustering) – antonio Jul 25 '16 at 08:59
  • antonio,Thanks a lot. –  Jul 25 '16 at 09:05