如果我将一组位置的纬度和经度存储在 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/