0

我有一个应用程序,我从数据库中获取经度和纬度,并希望将其显示在MapView. 我能够显示最近存储的那个,但我认为for循环中需要一些数组才能将其添加到叠加层中。我不知道如何实现它。

这是我的 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);
    }           
});
4

1 回答 1

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

对于上面的块,您必须在执行之前初始化这两项......而-

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


HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(
            drawable, this);

编辑——添加迭代光标部分——

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);
于 2013-03-07T08:26:52.027 回答