2016-03-15 41 views
0

標記由用戶放在地圖上,並存儲在使用SQLite存儲的ArrayList中。標記如下所示。 enter image description here如何通過點擊按鈕來繪製存儲在ArrayList中的標記?

當用戶點擊查看時,數據恢復如下。 enter image description here

當用戶點擊更新按鈕時,View上的所有標記都應該繪製在Map上。我應該如何使用哪些代碼來繪製ArrayList(LatLongposition)中的值? 我如何在地圖標記上繪製ArrayList(LatLongposition)?

private void UpdatData() { 
    btnUpdate.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      //LatLongPosition is a object of the ArrayList 
      boolean isUpdate = myDb.updateData(id.getText().toString(), LatLongPosition); 
      PolylineOptions polylineOptions = new PolylineOptions().addAll(LatLongPosition); 
      Polyline p = GoogleMap.addPolyline(polylineOptions); 
      p.setPoints(LatLongPosition); 
      /*if (marker1 == null) { 
       marker1 = GoogleMap.addMarker(new MarkerOptions() 
           .position(p) 
           .title("Start Position") 
           .snippet(String.valueOf(googleMap.getCameraPosition())) 
           .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)) 
       );*/ 
      if (isUpdate == true) 
       Toast.makeText(ActivityMapDisplay.this, "Data Updated", Toast.LENGTH_SHORT).show(); 
      else 
       Toast.makeText(ActivityMapDisplay.this, "Data Not Updated", Toast.LENGTH_SHORT).show(); 

     } 
    }); 
} 

回答

0

你只需要遍歷您的List

for (LatLng position : LatLongPosition) { 
    GoogleMap.addMarker(new MarkerOptions() 
      .position(position) 
      .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))); 
} 

無論如何,考慮到必須遵循編碼約定帳戶,和變量名必須以小寫字符開頭。因此,您的GoogleMap必須命名爲googleMap,並且您的LatLongPosition變量必須命名爲latLongPosition

+0

我應該在DatabaseHelper類上編寫一些代碼嗎?更新方法?因爲我確實把上面的代碼,但標記不被繪製。 –

+0

LatLng的ArrayList的名稱是什麼?您的ArrayList是否在您的onClick方法上爲空? – antonio

+0

它的私人最終ArrayList LatLongPosition = new ArrayList <>(); ArrayList在onClick方法中不是空的 –