3

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

Austyn Mahoney
  • 11,398
  • 8
  • 64
  • 85
April Smith
  • 1,810
  • 2
  • 28
  • 52

1 Answers1

6

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

Austyn Mahoney
  • 11,398
  • 8
  • 64
  • 85
  • A possibly slightly more efficient approach would be to hand the `Cursor` to the `ItemizedOverlay` and have it implement `size()` (returning `getCount()` from the `Cursor`) and `getItem()` (returning the result of creating an `OverlayItem` from the `Cursor` after a `moveToPosition()` call). This saves keeping your own `ArrayList`. But your approach would work as well, and would be particularly important if the `Cursor` might be closed while the map was still in use. – CommonsWare Sep 02 '11 at 18:10
  • I agree Mark. I debated posting that code, but the user seems to be lost on even getting data out of the database, so this generic code seemed to be a better choice. Once they figure out what I did here, they should be able to refactor the code into `getItem()`. You're also right that the posted code would be better if you had data that was static, no reason to keep an open `Cursor` around in that case. – Austyn Mahoney Sep 02 '11 at 18:14
  • Thank you so much. It works well. However i don't get the exactly point on map what i want..i don't know why. The pins move to the next city and the distance between each other extend 2 or 3 times This is and example i store in my database type String latitude = 18.481941 longitude = 98.551844 ... which i miss? anyway, thank you so much for your help – April Smith Sep 03 '11 at 01:24
  • Sorry for stupid question.. i already know the cause .. i put the wrong latitude and longitude :b – April Smith Sep 03 '11 at 03:16
  • @April you complete this type of demo then give me i also need you. – Roadies Dec 04 '12 at 11:22