我正在尝试计算两个位置之间的距离。一个位置的lat long现在是硬编码的(我稍后会通过考虑gps中的值来改变它),其他位置存储在数据库中。我使用以下查询
Cursor c = sampleDB.rawQuery("SELECT NAME, CONTACT,((LAT -" +
" LATCUR) * (LAT - LATCUR) + ((LONG - LONGCUR) * 2) * ((LONG - LONGCUR) * 2)) " +
"AS DISTANCE FROM AMBULANCE ORDER BY DISTANCE", null );
其中LATCUR
和LONGCUR
是当前值。我收到错误,因为没有名为latcur的列。我该怎么办?
我将latcur和longcur值声明为 public static double LATCUR = 39.991932; public static double LONGCUR = -83.012352;
代码: 公共类MainActivity扩展了ListActivity {
private final String DBNAME = "SERVICES";
private final String TABLENAME = "AMBULANCE";
public static double LATCUR = 39.991932;
public static double LONGCUR = -83.012352;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<String> results = new ArrayList<String>();
SQLiteDatabase sampleDB = null;
try {
sampleDB = this.openOrCreateDatabase(DBNAME, MODE_PRIVATE, null);
sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
TABLENAME +
" (NAME VARCHAR, CONTACT VARCHAR," +
" LAT float, LONG float, LATC double, LONGC double);");
Cursor c = sampleDB.rawQuery(“SELECT NAME,CONTACT,((LAT - ”+ “LATCUR)*(LAT - LATCUR)+((LONG - LONGCUR)* 2)*((LONG - LONGCUR)* 2))”+ “按距离划分的救援距离”,无效);
if (c != null ) {
if (c.moveToFirst()) {
do {
String NAME = c.getString(c.getColumnIndex("NAME"));
String CONTACT = c.getString(c.getColumnIndex("CONTACT"));
results.add("" + NAME + ",CONTACT: " + CONTACT);
}while (c.moveToNext());
}
}
this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));
}
答案 0 :(得分:1)
你引用了它们。您需要在引用的SQL字符串之外进行所有数学运算。
答案 1 :(得分:1)
试试这个......
Cursor c = sampleDB.rawQuery("SELECT NAME, CONTACT,((LAT -"+LATCUR+") * (LAT - "+LATCUR+") + ((LONG - "+LONGCUR+") * 2) * ((LONG - "+LONGCUR+") * 2)) " +
"AS DISTANCE FROM AMBULANCE ORDER BY DISTANCE", null );
希望它有所帮助。