2016-07-25 86 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