从Android中的数据库中获取经纬度值 [英] Getting lat long values from database in android

查看:90
本文介绍了从Android中的数据库中获取经纬度值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注先前的问题(位于此处:

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

我了解那里的代码,并且我的数据库具有相似的布局,因为它有4列; ID,名称,纬度和经度.我什至想做那里所说的事情,因为它从该表中选择了所有内容,然后将经纬度放置为覆盖图中的点.我的问题是答案需要具备一些先决条件,所以我只是想知道是否有人可以帮助我完成这项工作?

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.

预先感谢,这是我已编辑的代码的当前状态; <​​/p>

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

更具体地说,我在databaseObject.query中遇到错误,我认为这是因为我之前没有这些部分.

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

编辑.

为了更加清晰,目的是从phpmyadmin中的表中获取经度和纬度值,并在Google地图叠加层上的我的Android应用程序中显示它们.上面的代码显示的是我找到的示例,但由于代码需要一些部分而无法正常工作,因此无法正常工作

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

推荐答案

如果查询未返回任何记录,则游标将为空,并且do while语句将以任何方式执行,最终将引发错误.试试这个

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

已:

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

这篇关于从Android中的数据库中获取经纬度值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆