从数据库中检索位置并将其显示在MapView中

时间:2013-03-07 07:47:35

标签: android sqlite android-mapview

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

这是我的按钮点击它将获取纬度/经度并显示在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);
    }           
});

1 个答案:

答案 0 :(得分:0)

您可以尝试使用此解决方案 -

  1. 创建一个名为 LatLonPoint 的类 - 这基本上是从latlon转换为GeoPoint -

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


    创建一个Itemized Overlay -


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