2016-07-25 116 views
0

我想在谷歌地图上显示多个标记(可能为100或更多)。我从sqlite的然后阅读LATLNG我加入他们的谷歌地图,但告诉我只是一个标记Android - 谷歌地图 - 如何在地图上显示多个标记?

我必须使用主题AsyncTask

这里是我的代码:

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

的可能的复制[如何显示谷歌地图API V2上MapFragment多个标记?](http://stackoverflow.com/questions/13855049/how-to-show-multiple-markers-on-mapfragment-in-google-map-api-v2) –

回答

1

要初始化您markerPoints在每次迭代。

从do-while循环删除markerPoints = new ArrayList<LatLng>();并初始化它的声明ArrayList<LatLng> markerPoints = new ArrayList<LatLng>();

+0

哦,是的,被遗忘了。非常感谢antonio。 –

+0

antonio,当我运行我的应用时,我的观点很多,应用太慢。我该怎么办?我必须使用Thread或AsyncTask ...吗? –

+2

您可以(也必须)使用'AsyncTask'来查询数据库,但不幸的是标记绘图必须在主(UI)线程上完成,并且是非常繁重的操作。提高速度的唯一选择是试图减少绘制标记(地图元素)的数量。例如使用标记群集(https://developers.google.com/maps/documentation/android-api/utility/marker-clustering) – antonio