获取纬度和经度从SQLite数据库在Android的MapOverlay使用经度、纬度、数据库、SQLite

2023-09-05 00:33:38 作者:我很怪但我不坏

如果我储存了一堆位置的经度和纬度在SQLite数据库,我将如何检索这些值,并把它们每一个OverlayItem类在谷歌的地图code使用?

If I store the Latitude and Longitude of a bunch of locations in a SQLite Database, how would I retrieve these values and place them each in an OverlayItem class for use in Google's Map code?

数据库名称:数据库

表名称:地点

字段中的地点表:

ID 标题 说明 纬度 经度 id title description latitude longitude

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

How do I get the latitude and longitude data of each location and add it to an ArrayList itemOverlay?

你有什么想法或一个例子吗?谢谢你这么多。

Do you have any ideas or an example? Thanks you so much

推荐答案

您会想要做这样的查询:

You would want to do a query like this:

SELECT title, description, latitude, longitude
FROM place

可以在Android的就像这样:

Which can be done in Android like this:

    /* 
       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使用重的纬度/经度值presentation整数相乘的值。如果您已经填充数据库的时候照顾了这一点,跳过乘法,并使用 locationCursor.getInt(...)

You need to multiply the values by 1E6 because Android uses an integer representation of the lat/long values. If you already took care of this when populating the database, skip the multiplication and use locationCursor.getInt(...).