2

我在关注一个较早的问题(位于此处:Get Latitude and Longitude from SQLite database for use in Android MapOverlay

我了解那里的代码,并且我的数据库具有类似的布局,因为它有 4 列;id、名称、纬度和 lng。我什至想做那里所说的,因为它从该表中选择所有内容并将纬度长点放置在叠加层中。我的问题是答案需要一些先决条件,所以我只是想知道是否有人可以帮助我完成这项工作。

提前致谢,这是我编辑的代码的当前状态;

ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
        Cursor locationCursor = databaseObject.query("locations", new String[] {"image_id", "name", "lat", "lng" }, null, null, null, null);

        locationCursor.moveToFirst();
        do {
            String image_id = locationCursor.getString(locationCursor
                    .getColumnIndex("image_id"));
            String name = locationCursor.getString(locationCursor
                    .getColumnIndex("name"));
            int latitude = (int) (locationCursor.getDouble(locationCursor
                    .getColumnIndex("lat")) * 1E6);
            int longitude = (int) (locationCursor.getDouble(locationCursor
                    .getColumnIndex("lng")) * 1E6);
            items.add(new OverlayItem(new GeoPoint(latitude, longitude), image_id, name));
        } while (locationCursor.moveToNext());

更具体地说,我在 databaseObject.query 中遇到错误,我假设这是因为我没有在此之前的部件。

编辑。

为了更加清晰,目的是从我在 phpmyadmin 中的表中获取 lat 和 long 值,并将它们显示在我的 android 应用程序中的谷歌地图覆盖上。上面的代码是我找到的一个示例的显示,但由于代码在它之前需要一些部分,所以无法让它工作

4

2 回答 2

1

如果查询不返回任何记录,则游标将为空,并且do while无论如何都会执行语句,最终将引发错误。尝试这个

编辑::

        SQLiteDatabase databaseObject = null; 
        databaseObject = this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);
        ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
        Cursor locationCursor = databaseObject.query("locations", new String[] {"image_id", "name", "lat", "lng" }, null, null, null, null);


         while (locationCursor.moveToNext()){
            String image_id = locationCursor.getString(locationCursor
                    .getColumnIndex("image_id"));
            String name = locationCursor.getString(locationCursor
                    .getColumnIndex("name"));
            int latitude = (int) (locationCursor.getDouble(locationCursor
                    .getColumnIndex("lat")) * 1E6);
            int longitude = (int) (locationCursor.getDouble(locationCursor
                    .getColumnIndex("lng")) * 1E6);
            items.add(new OverlayItem(new GeoPoint(latitude, longitude), image_id, name));
        }
于 2012-04-25T00:59:28.353 回答
0

也许您应该在数据库服务器端用 php 编写一些代码,然后检索您的数据库,然后将结果作为 JSON 传递。看到这个链接这个

于 2012-04-25T02:17:14.510 回答