2013-03-07 151 views
0

我有一個應用程序,可以從我的數據庫中獲取經度和緯度,並且想要在MapView中顯示它。我能夠顯示最近存儲的那個,但我認爲在for循環中需要一些數組才能將其添加到疊加層中。我不知道如何實現它。從數據庫中檢索位置並在MapView中顯示它

這是我buttonClick,並會獲取緯度/長,顯示它MapView

buttonShowMarkers = (Button) findViewById(R.id.button_SHOW_MARKERS); 

buttonShowMarkers.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     try { 
      db = openOrCreateDatabase("LocationFetch.db", MODE_PRIVATE , null); 
      cursor = db.rawQuery("SELECT * FROM user" , null); 
      cursor.moveToFirst(); 
      do { 
       int dataID = cursor.getColumnIndex("id"); 
       String dataString = cursor.getString(0); 
       Log.d("ID DATA FROM DATABASE---->" , dataString); 

       String dataNAME = cursor.getString(1).toString().trim(); 
       Log.d("NAME DATA FROM DATABASE---->", dataNAME); 

       String dataLAT = cursor.getString(2).toString().trim(); 
       Log.d("LAT DATA FROM DATABASE----->" , dataLAT); 
       int latitude = cursor.getColumnIndex("latitude"); 

       String dataLON = cursor.getString(3).toString().trim(); 
       Log.d("LON DATA FROM DATABASE----->" , dataLON); 
       int longitude = cursor.getColumnIndex("longitude"); 

       showLatLon(latitude , longitude , dataNAME); 
       // ArrayList<OverlayItem> items = new ArrayList<OverlayItem>(); 
       /* List<Overlay> ListOverlays = mapView.getOverlays(); 
       * OverlayItem[] markerItem = {new OverlayItem(new GeoPoint(latitude, longitude) , ""+dataNAME , ""+dataString)}; 
       * OverlayItem markerItem = new OverlayItem(new GeoPoint((int)(latitude *1E6), (int)(longitude *1E6)) , ""+dataNAME , ""+dataString); 
       * drawableOne = getResources().getDrawable(R.drawable.location_blue);   
       * helloItemizedOverlay = new HelloItemizedOverlay(drawableOne); 
       * helloItemizedOverlay.addOverlayItem(markerItem); 
       * mapView.getOverlays().add(helloItemizedOverlay); 
       * ListOverlays.add(myItemizedOverlay);*/ 

       //for(int i = dataID ;) 
      } while (cursor.moveToNext()); 

      /*String[] arrLat = new String[]{LATdata}; 
       String[] arrLon = new String[]{LONdata};*/ 

      //showLatLon(dataLAT , dataLON);      
      cursor.moveToNext(); 
     } catch(SQLException e) { 
      e.printStackTrace(); 
     } finally { 
      cursor.close(); 
     } 
     db.close(); 
    }   

    private void showLatLon(int latitude , int longitude , String nAMEdata) { 
     // GeoPoint point = new GeoPoint((latitude) , (latitude)); 

     List<Overlay> ListOverlays = mapView.getOverlays(); 

     //OverlayItem[] markerItem = {new OverlayItem(new GeoPoint(latitude, longitude) , ""+dataNAME , ""+dataString)}; 
     OverlayItem markerItem = new OverlayItem(new GeoPoint(latitude,latitude) , ""+nAMEdata , null); 
     drawableOne = getResources().getDrawable(R.drawable.location_blue); 

     helloItemizedOverlay = new HelloItemizedOverlay(drawableOne);   
     helloItemizedOverlay.addOverlayItem(markerItem); 

     mapView.getOverlays().add(helloItemizedOverlay); 

     ListOverlays.add(myItemizedOverlay); 
    }   
}); 
+0

MapView?您有沒有使用Android Gmaps API v2的原因? – yarian 2013-03-07 07:52:12

+0

谷歌地圖Android API版本1自2012年12月3日起正式棄用。鼓勵開發者使用Google Maps Android API v2。 – Anukool 2013-03-07 08:09:01

+0

我正在使用薑餅 – ayansinha 2013-03-07 09:44:00

回答

0

您可以嘗試使用該解決方案 -

  1. 創建一個類名爲LatLonPoint - 這基本上是從latlon轉換到GeoPoint -


public class LatLonPoint extends GeoPoint { 
public LatLonPoint(double latitude, double longitude) { 
    super((int) (latitude * 1E6), (int) (longitude * 1E6)); 
} 
} 


創建一個分項覆蓋 - 在主要活動


public class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem> { 
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>(); 
private Context mContext; 

public HelloItemizedOverlay(Drawable defaultMarker, Context context) { 
    super(boundCenterBottom(defaultMarker)); 
    mContext = context; 
} 

public void addOverlay(OverlayItem overlay) { 
    mOverlays.add(overlay); 
    populate(); 
} 

@Override 
protected OverlayItem createItem(int i) { 
    return mOverlays.get(i); 
} 

@Override 
public int size() { 
    return mOverlays.size(); 
} 

@Override 
protected boolean onTap(int index) { 
    OverlayItem item = mOverlays.get(index); 
    AlertDialog.Builder dialog = new AlertDialog.Builder(mContext); 
    dialog.setTitle(item.getTitle()); 
    dialog.setMessage(item.getSnippet()); 
    dialog.show(); 
    return true; 
} 

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event, MapView mapView) { 

    if (keyCode == KeyEvent.KEYCODE_BACK) { 

    } 
    return super.onKeyDown(keyCode, event, mapView); 
} 

}

現在 -

try { 
      db = openOrCreateDatabase("LocationFetch.db", MODE_PRIVATE , null); 
      cursor = db.rawQuery("SELECT * FROM user" , null); 
      cursor.moveToFirst(); 

      do { 
       int dataID = cursor.getColumnIndex("id"); 
       String dataString = cursor.getString(0); 
       Log.d("ID DATA FROM DATABASE---->" , dataString); 

       String dataNAME = cursor.getString(1).toString().trim(); 
       Log.d("NAME DATA FROM DATABASE---->", dataNAME); 

       String dataLAT = cursor.getString(2).toString().trim(); 
       Log.d("LAT DATA FROM DATABASE----->" , dataLAT); 
       int latitude = cursor.getColumnIndex("latitude"); 

       String dataLON = cursor.getString(3).toString().trim(); 
       Log.d("LON DATA FROM DATABASE----->" , dataLON); 
       int longitude = cursor.getColumnIndex("longitude"); 

        // Add the item here -- 
       itemizedoverlay.addOverlay(new OverlayItem(new LatLonPoint(latitude, longitude), dataNAME, null)); 


       //showLatLon(latitude , longitude , dataNAME); 
       // ArrayList<OverlayItem> items = new ArrayList<OverlayItem>(); 
       /* List<Overlay> ListOverlays = mapView.getOverlays(); 
       * OverlayItem[] markerItem = {new OverlayItem(new GeoPoint(latitude, longitude) , ""+dataNAME , ""+dataString)}; 
       * OverlayItem markerItem = new OverlayItem(new GeoPoint((int)(latitude *1E6), (int)(longitude *1E6)) , ""+dataNAME , ""+dataString); 
       * drawableOne = getResources().getDrawable(R.drawable.location_blue);   
       * helloItemizedOverlay = new HelloItemizedOverlay(drawableOne); 
       * helloItemizedOverlay.addOverlayItem(markerItem); 
       * mapView.getOverlays().add(helloItemizedOverlay); 
       * ListOverlays.add(myItemizedOverlay);*/ 

       //for(int i = dataID ;) 
      } while (cursor.moveToNext()); 

       // Add all itemized overlay here 
      mapOverlays.add(itemizedoverlay); 


      /*String[] arrLat = new String[]{LATdata}; 
       String[] arrLon = new String[]{LONdata};*/ 

      //showLatLon(dataLAT , dataLON);      
      //cursor.moveToNext(); 
     } catch(SQLException e) { 
      e.printStackTrace(); 
     } finally { 
      cursor.close(); 
     } 

對於你必須初始化這兩個項目beffore的做上述塊。雖然 -

List<Overlay> mapOverlays = mMapView.getOverlays(); 


HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(
      drawable, this); 

EDIT--添加迭代遊標部分 -

Drawable drawable = this.getResources().getDrawable(
        R.drawable.ic_launcher); 
      List<Overlay> mapOverlays = mMapView.getOverlays(); 
      HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(
        drawable, this); 
      while(cursor.moveToNext()) 
      { 
       // get the values -- 
       int dataID = cursor.getColumnIndex("id"); 

       String dataString = cursor.getString(0); 
       Log.d("ID DATA FROM DATABASE---->" , dataString); 

       String dataNAME = cursor.getString(1).toString().trim(); 
       Log.d("NAME DATA FROM DATABASE---->", dataNAME); 

       String dataLAT = cursor.getString(2).toString().trim(); 
       Log.d("LAT DATA FROM DATABASE----->" , dataLAT); 

       int latitude = cursor.getColumnIndex("latitude"); 
       String dataLON = cursor.getString(3).toString().trim(); 
       Log.d("LON DATA FROM DATABASE----->" , dataLON); 
       int longitude = cursor.getColumnIndex("longitude"); 

       // Assuming your logic for retrieving values is correct 
       // Add into the itemized Overlay here 
       itemizedoverlay.addOverlay(new OverlayItem(new LatLonPoint(latitude, longitude), dataNAME, null)); 

      } 
      mapOverlays.add(itemizedoverlay); 
+0

@Ankool它不工作親愛的,它給我錯誤光標OutOfBoundException。即使我在外圈寫字,我將如何獲得經度和緯度,再次幫助我SOS。 – ayansinha 2013-03-07 09:42:43

+0

不知何故,我認爲第二個cursor.moveToNext()不是必需的。嘗試評論一個。檢查編輯。 – Anukool 2013-03-07 09:54:24

+0

嗯,我已經調試了這個異常,但現在只能獲取我輸入數據庫的最後一個值,即最近的數據。以及如何在地圖視圖中顯示所有文件和長文件。 – ayansinha 2013-03-07 10:01:05

相關問題