【问题标题】:Getting lat long values from database in android从android中的数据库获取lat long值
【发布时间】:2012-04-25 00:38:43
【问题描述】:

我在关注一个较早的问题(位于此处:Get Latitude and Longitude from SQLite database for use in Android MapOverlay

我了解那里的代码,并且我的数据库具有类似的布局,因为它有 4 列; id、名称、纬度和 lng。我什至想做那里所说的,因为它从该表中选择所有内容并将纬度长点放置在叠加层中。我的问题是答案需要一些先决条件,所以我只是想知道是否有人可以帮助我完成这项工作。

提前致谢,这是我编辑的代码的当前状态;

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 中遇到错误,我假设这是因为我没有在此之前的部件。

编辑。

为了更加清晰,目的是从我在 phpmyadmin 中的表中获取 lat 和 long 值,并将它们显示在我的 android 应用程序中的谷歌地图覆盖上。上面的代码是我找到的一个示例的显示,但由于代码之前需要一些部分,所以无法使其工作

【问题讨论】:

    标签: android google-maps latitude-longitude


    【解决方案1】:

    如果查询没有返回任何记录,则光标将为空,do while 语句无论如何都会被执行,最终会引发错误。试试这个

    已编辑::

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

    【讨论】:

    • 感谢您的快速回复,但是我仍然收到有关 databaseObject 的错误,因为我之前缺少有关 getReadableDatabase(); 的代码;我不确定这是否是正确的方法,因为我已经访问了另一个表的数据库以使用 httppost 检查登录详细信息
    • 这行当然可以;光标位置Cursor = databaseObject.query("locations", new String[] {"image_id", "name", "lat", "lng" }, null, null, null, null);无法解析 databaseObject.query,此代码的问题是它要求我在代码中有一个数据库对象和 getReadableDatabase() 方法,我不知道该怎么做。我已经使用 phpmyadmin 创建了一个表格,上面保存了这些值,我想从该表格中获取纬度和经度值并将它们放在 googlemaps 叠加层上。上面的代码是我使用 SQLite 找到的一个示例。
    • 你能告诉我你在哪里创建'databaseObject'并连接数据库吗?
    • 这就是问题所在,在这个问题中,我正在寻求任何帮助,因为我不确定 - 我正在编写代码以显示我试图用它做什么:) - 对不起,如果不清楚。
    • 我的错,请通过link 这将告诉您如何创建数据库并在您的 Android 应用程序中使用。这也有源代码。该链接位于博客底部,您可以从那里下载。希望它会有所帮助。如果您还有其他问题,请在此处回复。
    【解决方案2】:

    也许您应该在数据库服务器端用 php 编写一些代码,然后检索您的数据库,然后将结果作为 JSON 传递。见this linkthis

    【讨论】:

    • 感谢您的意见。我之前遇到过这个例子,我确实在服务器端编写了一些 php 代码来从数据库中选择数据,但是就我所知,因为在编写它之前我从未使用过 JSON。我担心的是能够通过在叠加层中添加地理点来使用它。
    • 只需解析您在 Android 应用中收到的 JSON,并将它们添加到您的叠加层中。有什么问题...
    • 阅读第一个链接中的代码我对它的工作原理有一个模糊的理解(我对 JSON 不太熟悉),我猜最后部分是以那种格式在Log.i 括号。我的困惑来自于操纵它以使用覆盖和地理点。
    猜你喜欢
    • 1970-01-01
    • 2019-04-28
    • 1970-01-01
    • 2015-01-25
    • 2015-05-20
    • 1970-01-01
    • 2012-03-20
    • 1970-01-01
    • 2011-11-28
    相关资源
    最近更新 更多