How to calculate distance from different markers in a map and then pick up the least one

后端 未结 9 1030
野趣味
野趣味 2020-12-05 08:53

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

9条回答
  •  悲哀的现实
    2020-12-05 09:20

    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(); 
         }
    }
    

    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();
    
    }
    
    }
    

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

提交回复
热议问题