如何计算地图中不同标记的距离,然后选择至少一个 [英] How to calculate distance from different markers in a map and then pick up the least one

查看:14
本文介绍了如何计算地图中不同标记的距离,然后选择至少一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须从地图上的不同标记到设备当前位置的距离,然后选择最短的.我有标记的纬度和经度,并且可以动态获取当前位置经度和经度.

I have to get distance from different markers on the map to the current location of the device and the pick up the shortest one. I have the lat and long for the markers and the current location lat and long can be fetched dynamically.

假设我在地图上有 5 个标记,班加罗尔 (Lat : 12.971599, Long : 77.594563), Delhi (Lat : 28.635308, Long : 77.224960), Mumbai (Lat : 19.075984, Long : 72.877656), Chennai (Lat : 13.,长:80.250825),加尔各答(纬度:22.572646,长:88.363895).

Suppose I have 5 markers on the map, Bangalore (Lat : 12.971599, Long : 77.594563), Delhi (Lat : 28.635308, Long : 77.224960), Mumbai (Lat : 19.075984, Long : 72.877656), Chennai (Lat : 13.052414, Long : 80.250825), Kolkata (Lat : 22.572646, Long : 88.363895).

现在假设用户站在海得拉巴附近的某个地方(纬度:17.385044,经度:78.486671).当用户单击按钮时,应用程序应计算与每个标记的距离并拾取并返回最短的标记,此处为班加罗尔.

Now suppose the user is standing somewhere near Hyderabad (Lat : 17.385044, Long : 78.486671). When the user clicks the button, the app should calculate distance from each marker and pick up and return the shortest one, that will be Bangalore here.

有一种方法可以在本地数据库的帮助下完成.有人可以帮忙吗?

There is a way possible to do it with help of local databases. Can anyone help on that please.?

任何人都可以建议我一个很好的方法来做到这一点,或者如果可以的话,请提供一个好的代码.提前谢谢.

Can anyone suggest me a nice way to do this, or come up with a good code if you please can. Thanx in advance.

推荐答案

在这里,我找到了使用数据库的方法.这是一个计算距离函数:

Here, I got a way to do that Using databases. This is a calculate distance function:

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] 的数组,其中包含您要计算距离的所有地点的纬度和经度.然后创建一个数据库,如下图:

All we have to do is Create an array named targetLatitude[i] and targetLongitude[i] containing Lats and Longs of all the places you want to calculate distance from. Then create a database as shown below:

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函数,即可得到最近的车站名称.

Then execute the CalculateDistance function wherever you want and you can get the nearest station name.

这篇关于如何计算地图中不同标记的距离,然后选择至少一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆