所以你有兩個座標點計算距離的代碼? 我用網頁製作這樣的東西。 您可以將所有座標保存到數據庫中,當您擁有手機位置時,可以取回位置附近的所有座標(如過濾器)。 之後,您可以使用您的代碼來檢查女巫座標是否在您的範圍X內,並將其放入包含座標的類中。
你的座標類:
public class Coordinates {
private double _lat;
private double _lng;
//Empty constructor
public Coordinates(){
}
//Constructor
public Coordinates(int id, double lat, double lng){
this._lat = lat;
this._lng = lng;
}
// constructor
public Materia(double lat, double lng){
this._lat = lat;
this._lng = lng;
}
// getting ID
public int getID(){
return this._id;
}
// setting id
public void setID(int id){
this._id = id;
}
//getting latitude
public int getLat(){
return this._lat;
}
//setting color
public void setLat(double lat){
this._lat = lat;
}
//getting longitude
public Double getLng() {
return this._lng;
}
//setting longitude
public void setLng(Double lng) {
this._lng = lng;
}
}
您的數據庫SQLite的:
public class MySQLiteHelper extends SQLiteOpenHelper {
//Database version
private static final int DATABASE_VERSION = 1;
//Database name
private static final String DATABASE_NAME = "coordinates.db";
//Materie table name
public static final String TABLE_COORDINATES = "coordinates";
//coordinates columns table names
public static final String COLUMN_ID = "_id";
public static final String COLUMN_LAT = "latitude";
public static final String COLUMN_LNG = "longitude";
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
onCreate(){}
onUpdate(){}
//Other methods that you need
//Here something like
// Getting All Coordinates
public List<Coordinate> getAllCoordinate() {
List<Coordinate> coordinateList = new ArrayList<Coordinate>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_COORDINATES;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Coordinate coordinate = new Coordinate();
coordinate.setID(Double.parseDouble(cursor.getString(0)));
coordinate.setLat(Double.parseDouble(cursor.getString(1)));
coordinate.setLng(Double.parseDouble(cursor.getString(2)));
// Adding contact to list
coordinateList.add(coordinate);
} while (cursor.moveToNext());
}
// return coordinate list
return coordinateList;
}
隨着getAllCoordinate你把所有數據庫中的協調和生成對象的數組,你可以用閱讀和訪問單個lat
和lng
。 也許代碼是不完美的,如果你使用,照顧一些錯誤。 Here a good guide for database