Get Latitude and Longitude from SQLite database for use in Android MapOverlay

眉间皱痕 提交于 2020-01-11 06:39:05

问题


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?

Database name: database

Table name: place

Fields in place Table:

  • id
  • title
  • description
  • latitude
  • longitude

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


回答1:


You would want to do a query like this:

SELECT title, description, latitude, longitude
FROM place

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());

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(...).



来源:https://stackoverflow.com/questions/7286746/get-latitude-and-longitude-from-sqlite-database-for-use-in-android-mapoverlay

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!