Getting lat long values from database in android

折月煮酒 提交于 2019-12-10 23:49:59

问题


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


回答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));
        }



回答2:


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.



来源:https://stackoverflow.com/questions/10307904/getting-lat-long-values-from-database-in-android

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