2

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

Community
  • 1
  • 1

2 Answers2

1

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));
        }
Rakesh
  • 3,987
  • 10
  • 43
  • 68
  • Thank you for the quick reply, however I still get the error around the databaseObject because I am missing the code before hand regarding getReadableDatabase(); I am not sure if this is the correct way to go about it as I already access the database for a different table to check login details using httppost – user1354861 Apr 25 '12 at 01:10
  • Sure with this line; Cursor locationCursor = databaseObject.query("locations", new String[] {"image_id", "name", "lat", "lng" }, null, null, null, null); The databaseObject.query cannot be resolved, the problem with this code is that it requires me to have a database object and the getReadableDatabase() method within the code which I do not know how to do. I have created a table using phpmyadmin holding these values above and I want to take the latitude and longitude values from that table and place them on a googlemaps overlay. The code above was an example I found using SQLite. – user1354861 Apr 25 '12 at 01:48
  • Can you please show me the line where you are creating 'databaseObject' and connecting with database? – Rakesh Apr 25 '12 at 01:52
  • That is the problem, in the question I was asking for any help on doing this because I am unsure - I was writing the code to show what I was trying to do with it :) - Sorry if it was unclear. – user1354861 Apr 25 '12 at 01:55
  • my bad, Please go through this [link](http://saigeethamn.blogspot.com/2009/10/android-developer-tutorial-part-12.html) this will tell you how to create database and use in your Android application. This also has source code. The link is provided at the bottom of the blog you can download it from there. Hope it will help. If you have further question please reply here. – Rakesh Apr 25 '12 at 02:10
  • I have a table created using phpmyadmin I just need to connect to it and get the data. Correct me if I am wrong but do I just use this line; sampleDB = this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null); and then the query in the above code? – user1354861 Apr 25 '12 at 02:14
  • yes it should work, provided that you are 'SQLiteDatabase sampleDB = null; sampleDB = this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null); ' in class which is extended with Activity class. – Rakesh Apr 25 '12 at 02:30
0

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.

Community
  • 1
  • 1
Hong Duan
  • 4,234
  • 2
  • 27
  • 50
  • Thank you for your input. I have come across this example before and I did write some php code server-side to select the data from the database, however that is as far as I got with it as I have never used JSON before the time of writing it. My concern was being able to use that with adding it with geopoints in an overlay. – user1354861 Apr 25 '12 at 02:29
  • Just parse the JSON you received in your Android app, and add them into your overlay. What's the problem... – Hong Duan Apr 25 '12 at 02:44
  • Reading the code in the first link I have a vague understanding of how it works(I am not too familiar with JSON) and I'm guessing that the end part is outputting it in that format within the Log.i bracket. My confusion comes with manipulating that to work with the overlay and geopoints. – user1354861 Apr 25 '12 at 03:35