【问题标题】:How can i query lat/long value from sqlite database and show on Google map如何从 sqlite 数据库中查询 lat/long 值并在 Google 地图上显示
【发布时间】:2012-04-04 13:33:18
【问题描述】:

如何从数据库中查询纬度/经度值并在该位置显示标记? 这是我的代码,当我设置纬度/经度时它工作正常

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

【问题讨论】:

  • 客户端应用程序是用什么写的?
  • 很抱歉错过了这些信息,我正在使用 android :)
  • 使用覆盖类并将图标设置为特定检索点
  • 我刚刚添加了我的代码,顶部工作正常,但是当我想从数据库中获取纬度/经度时,它根本不起作用 --
  • 您需要比“根本不起作用”更具体 - 您的应用程序会崩溃吗?例外?你得到虚假数据吗?您是否尝试过记录您获得的输出?您是否确保数据库看起来像您期望的那样?很难帮你。

标签: android sqlite google-maps


【解决方案1】:
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());
            } 
        } 

试试这个代码

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-29
    • 1970-01-01
    • 2018-11-21
    • 1970-01-01
    • 2016-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多