如何从sqlite数据库查询lat / long值并在Google地图上显示

时间:2011-04-27 06:47:37

标签: android sqlite google-maps

如何从数据库中查询lat / long值并在该位置显示标记? 这是我的代码,当我设置lat / long

时,它工作正常
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.branchmap);
    mapView = (MapView) findViewById(R.id.mapView1);


      mc = mapView.getController();
          String[] coordinates = {"13.694317","100.601592"};
            double lat = Double.parseDouble(coordinates[0]);
            double lng = Double.parseDouble(coordinates[1]);

            p = new GeoPoint(
                    (int) (lat * 1E6), 
                    (int) (lng * 1E6));
            mc.animateTo(p);
            mc.setZoom(17); 


            mapOverlays = mapView.getOverlays();
            drawable = this.getResources().getDrawable(R.drawable.mark1);
            itemizedOverlay = new HelloItemizedOverlay(drawable);
            OverlayItem overlayitem = new OverlayItem(p, "", "");
            itemizedOverlay.addOverlay(overlayitem);
            mapOverlays.add(itemizedOverlay);

但是当我想查询它不起作用时,我应该把这部分代码放在哪里

try {
        ChannelDatabaseHelper dbHelper = new  ChannelDatabaseHelper(this.getApplicationContext());
        newDB = dbHelper.getWritableDatabase();
        Cursor c1 = newDB.rawQuery("SELECT lat,long FROM " +
                tableName , null);

        if (c1 != null ) {
            if  (c1.moveToFirst()) {
                do {

                    latitude =Integer.parseInt((c1.getString(c1.getColumnIndex("lat"))));
                    longitude = Integer.parseInt((c1.getString(c1.getColumnIndex("long"))));
                    p = new GeoPoint(
                            (int)(latitude *1E6 ), 
                            (int)(longitude *1E6 ));
                }while (c1.moveToNext());
            } 
        } 

1 个答案:

答案 0 :(得分:0)

try {
        ChannelDatabaseHelper dbHelper = new  ChannelDatabaseHelper(this.getApplicationContext());
        newDB = dbHelper.getWritableDatabase();
        Cursor c1 = newDB.rawQuery("SELECT lat,long FROM " +
                tableName , null);

        if (c1 != null ) {
            if  (c1.moveToFirst()) {
                do {

                    latitude =Integer.parseDouble((c1.getString(c1.getColumnIndex("lat"))));
                    longitude = Integer.parseDouble((c1.getString(c1.getColumnIndex("long"))));
                    p = new GeoPoint(
                            (int)(latitude *1E6 ), 
                            (int)(longitude *1E6 ));
                    //Write a code to display this point on google-map
                }while (c1.moveToNext());
            } 
        } 

试用此代码