android - 从 SQLite 数据库获取纬度和经度以在 Android MapOverlay 中使用

标签 android database sqlite android-maps mapactivity

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

数据库名称:数据库

表名:地方

place表中的字段:

  • id
  • 标题
  • 描述
  • 纬度
  • 经度

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

您有任何想法或示例吗?非常感谢你

最佳答案

你会想做这样的查询:

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 使用整数表示纬度/经度值。如果您在填充数据库时已经解决了这个问题,请跳过乘法并使用 locationCursor.getInt(...)

关于android - 从 SQLite 数据库获取纬度和经度以在 Android MapOverlay 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7286746/

相关文章:

java - 如何在 SQLite 中使用外键插入值?

java - Button.setOnclickListener(this) 的问题

android - 仅使用互联网而非 GPS 查找 Android 设备的大致位置

C#生成唯一的Guid(不存在于数据库的表中)

sqlite - iOS 上 TDateTime 的舍入错误

android - 在 android 中更改现有 db4o 中的类属性

android - Android 上的 Opera Mobile 不打开应用程序(不遵守 intent-filter)

java - CustomListview中的NullPointer原因

mysql - 如果 mysql 中不存在则设置另一个值

javascript - 将日期输入 SQL 数据库