SQLite无法从SQLite错误查询我附近的位置。不存在此类acos函数

时间:2014-05-27 06:49:46

标签: android android-sqlite

    public ArrayList<HashMap<String, String>> SelectGraphnearmeData(String lat,
            String lng) {

    try {

        ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>     ();
        HashMap<String, String> map;

        SQLiteDatabase db;
        db = this.getReadableDatabase(); // Read Data

        Cursor cursor = db.rawQuery(
                "SELECT *, ( 3959 * acos( cos( radians('"
                + lat
                + "') ) * cos( radians( a.latitude ) ) * cos( radians( a.longitude ) - radians('"
                + lng
                + "') ) + sin( radians('"
                + lat
                + "') ) * sin( radians( a.latitude ) ) ) ) AS distanceFROM station AS a HAVING distance < 1000 ORDER BY distance",
                null);

       if (cursor != null) {
           if (cursor.moveToFirst()) {
               do {

                   map = new HashMap<String, String>();
                   map.put("id", cursor.getString(0));
                   map.put("stationid", cursor.getString(1));
                   map.put("gaslog", cursor.getString(5));
                   map.put("petrolog", cursor.getString(6));
                   map.put("ngvlog", cursor.getString(7));
                   map.put("log_date_length", cursor.getString(20));
                   map.put("log_date", cursor.getString(21));

                   MyArrList.add(map);
               } while (cursor.moveToNext());
           }
       }

我有问题。我想在Android应用程序中获取最近的谷歌地图站。不同的点/坐标存储在sqlite数据库中。我必须从他们那里得到最接近的5。我使用的查询是:

1 个答案:

答案 0 :(得分:0)

这是我在项目中用来查找距离并将其放入sqlite数据库的代码。正如评论者所提到的,您无法在SQL中执行这些计算。

Note: MyData = ArrayList<ArrayList<String>> is data I had pulled from SQL

    for (ArrayList<String> item : MyData) {
        Location itemloc = new Location("storedData");
        itemloc.setLatitude(Double.valueOf(item.get(2)));
        itemloc.setLongitude(Double.valueOf(item.get(3)));
        //do something with my new location
        double distance = calcDistance(itemloc,"mi");
        ContentValues cv = new ContentValues();
        cv.put("Distance",distance);
        db.update("DataList",cv,"ItemID='" + item.get(0) + "'",null);
    }

private double calcDistance(Location newloc, String scale) {
    double factor = 0;
    if (scale.equals("km")) {factor = 1000;}
    else if (scale.equals("mi")) {factor = 1609.34;}
    double distance = newloc.distanceTo(savedlocation)/factor;
    return distance;
}