3

如果我将一堆位置的纬度和经度存储在 SQLite 数据库中,我将如何检索这些值并将它们分别放置在 OverlayItem 类中以在 Google 的地图代码中使用?

数据库名称:database

表名:place

表中的字段place

  • id
  • title
  • description
  • latitude
  • longitude

如何获取每个位置的经纬度数据并将其添加到 ArrayList itemOverlay?

你有什么想法或例子吗?非常感谢

4

1 回答 1

6

你会想做这样的查询:

SELECT title, description, latitude, longitude
FROM place

这可以像这样在Android中完成:

    /* 
       databaseObject = YourDbHelper#getReadableDatabase();
    */
    ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
    Cursor locationCursor = databaseObject.query("place", new String[]{
            "title", "description", "latitude", "longitude"}, null, null,
            null, null, null);

    locationCursor.moveToFirst();

    do {
        String title = locationCursor.String(locationCursor
                .getColumnIndex("title"));
        String description = locationCursor.String(locationCursor
                .getColumnIndex("description"));
        int latitude = (int) (locationCursor.getDouble(locationCursor
                .getColumnIndex("latitude")) * 1E6);
        int longitude = (int) (locationCursor.getDouble(locationCursor
                .getColumnIndex("longitude")) * 1E6);

        items.add(new OverlayItem(new GeoPoint(latitude, longitude), title,
                description));
    } while (locationCursor.moveToNext());

您需要将这些值乘以 1E6,因为 Android 使用 lat/long 值的整数表示。如果您在填充数据库时已经考虑到这一点,请跳过乘法并使用locationCursor.getInt(...).

于 2011-09-02T18:08:21.760 回答