user1354861
user1354861

Reputation: 21

Getting lat long values from database in android

I was following an earlier question (Located here: Get Latitude and Longitude from SQLite database for use in Android MapOverlay)

I understand the code there and my database has a similar layout in that it has 4 columns; id, name, lat and lng. I even want to do what is said there in that it selects all from that table and places the lat long as points in an overlay. My problem is the answer requires some pre requisites so I was just wondering if anyone could give me some help in what is needed to make that work.

Thanks in advance and here is the current state of code I have edited;

ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
        Cursor locationCursor = databaseObject.query("locations", new String[] {"image_id", "name", "lat", "lng" }, null, null, null, null);

        locationCursor.moveToFirst();
        do {
            String image_id = locationCursor.getString(locationCursor
                    .getColumnIndex("image_id"));
            String name = locationCursor.getString(locationCursor
                    .getColumnIndex("name"));
            int latitude = (int) (locationCursor.getDouble(locationCursor
                    .getColumnIndex("lat")) * 1E6);
            int longitude = (int) (locationCursor.getDouble(locationCursor
                    .getColumnIndex("lng")) * 1E6);
            items.add(new OverlayItem(new GeoPoint(latitude, longitude), image_id, name));
        } while (locationCursor.moveToNext());

More specifically I get an error with the databaseObject.query which im assuming is because I dont have the parts before this.

Edit.

For some extra clarity the aim is to get the lat and long values from my table in phpmyadmin and show them in my android application on a google maps overlay. The above code is a display of an example that I found but was unable to get it working because the code required some parts before it

Upvotes: 2

Views: 1915

Answers (2)

Rakesh
Rakesh

Reputation: 4127

In case query doesn't return any records the cursor will be empty and do while statement will get executed anyhow and which will eventually throw errors. Try this

Edited::

        SQLiteDatabase databaseObject = null; 
        databaseObject = this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);
        ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
        Cursor locationCursor = databaseObject.query("locations", new String[] {"image_id", "name", "lat", "lng" }, null, null, null, null);


         while (locationCursor.moveToNext()){
            String image_id = locationCursor.getString(locationCursor
                    .getColumnIndex("image_id"));
            String name = locationCursor.getString(locationCursor
                    .getColumnIndex("name"));
            int latitude = (int) (locationCursor.getDouble(locationCursor
                    .getColumnIndex("lat")) * 1E6);
            int longitude = (int) (locationCursor.getDouble(locationCursor
                    .getColumnIndex("lng")) * 1E6);
            items.add(new OverlayItem(new GeoPoint(latitude, longitude), image_id, name));
        }

Upvotes: 1

Hong Duan
Hong Duan

Reputation: 4294

Maybe you should write some code in php at the database server-side, and retrieve your database, then pass the results as a JSON. see this link and this.

Upvotes: 0

Related Questions