在这里,我有一种方法可以使用数据库。这是一个计算距离函数:
public void calculateDistance() {
if (latitude != 0.0 && longitude != 0.0) {
for(int i=0;i<97;i++) {
Location myTargetLocation=new Location("");
myTargetLocation.setLatitude(targetLatitude[i]);
myTargetLocation.setLongitude(targetLongitude[i]);
distance[i]=myCurrentLocation.distanceTo(myTargetLocation);
distance[i]=distance[i]/1000;
mdb.insertDetails(name[i],targetLatitude[i], targetLongitude[i], distance[i]);
}
Cursor c1= mdb.getallDetail();
while (c1.moveToNext()) {
String station_name=c1.getString(1);
double latitude=c1.getDouble(2);
double longitude=c1.getDouble(3);
double dis=c1.getDouble(4);
//Toast.makeText(getApplicationContext(),station_name+" & "+latitude+" & "+longitude+" & "+dis,1).show();
}
Arrays.sort(distance);
double nearest_distance=distance[0];
Cursor c2=mdb.getNearestStationName();
{
while (c2.moveToNext()) {
double min_dis=c2.getDouble(4);
if(min_dis==nearest_distance)
{
String nearest_stationName=c2.getString(1);
if(btn_clicked.equals("source"))
{
source.setText(nearest_stationName);
break;
}
else if(btn_clicked.equals("dest"))
{
destination.setText(nearest_stationName);
break;
}
else
{
}
}
}
}
}
else
{
Toast.makeText(this, "GPS is Not Working Properly,, please check Gps and Wait for few second", 1).show();
}
}
我们所要做的就是创建一个名为 targetLatitude[i] 和 targetLongitude[i] 的数组,其中包含您要计算距离的所有地点的纬度和经度。然后创建一个数据库,如下图:
public class MyDataBase {
SQLiteDatabase sdb;
MyHelper mh;
MyDataBase(Context con)
{
mh = new MyHelper(con, "Metro",null, 1);
}
public void open() {
try
{
sdb=mh.getWritableDatabase();
}
catch(Exception e)
{
}
}
public void insertDetails(String name,double latitude,double longitude,double distance) {
ContentValues cv=new ContentValues();
cv.put("name", name);
cv.put("latitude", latitude);
cv.put("longitude",longitude);
cv.put("distance", distance);
sdb.insert("stations", null, cv);
}
public void insertStops(String stop,double latitude,double logitude)
{
ContentValues cv=new ContentValues();
cv.put("stop", stop);
cv.put("latitude", latitude);
cv.put("logitude", logitude);
sdb.insert("stops", null, cv);
}
public Cursor getallDetail()
{
Cursor c=sdb.query("stations",null,null,null,null,null,null);
return c;
}
public Cursor getNearestStationName() {
Cursor c=sdb.query("stations",null,null,null,null,null,null);
return c;
}
public Cursor getStops(String stop)
{
Cursor c;
c=sdb.query("stops",null,"stop=?",new String[]{stop},null, null, null);
return c;
}
class MyHelper extends SQLiteOpenHelper
{
public MyHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("Create table stations(_id integer primary key,name text," +
" latitude double, longitude double, distance double );");
db.execSQL("Create table stops(_id integer primary key,stop text," +
"latitude double,logitude double);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
public void deleteDetail() {
sdb.delete("stations",null,null);
sdb.delete("stops",null,null);
}
public void close() {
sdb.close();
}
}
然后在任何你想要的地方执行CalculateDistance函数,你可以获得最近的车站名称。