从android中的数据库获取lat long值

时间:2012-04-25 00:38:43

标签: android google-maps latitude-longitude

我正在关注之前的问题(位于此处:Get Latitude and Longitude from SQLite database for use in Android MapOverlay

我理解那里的代码,我的数据库有类似的布局,因为它有4列; id,name,lat和lng。我甚至想要做那里所说的,因为它从该表中选择所有并将lat作为叠加点。我的问题是答案需要一些先决条件,所以我只是想知道是否有人可以帮助我做一些有用的工作。

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

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值,并在google地图叠加层上的Android应用程序中显示它们。上面的代码是我发现的一个示例的显示,但无法使其工作,因为代码需要一些部分才能使用

2 个答案:

答案 0 :(得分: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));
        }

答案 1 :(得分:0)

也许你应该在数据库服务器端用php编写一些代码,并检索你的数据库,然后将结果作为JSON传递。请参阅this linkthis

相关问题