2016-03-29 93 views
-3

我想要你的幫助來計算兩個經度的距離。任何人都可以告訴我如何找到距離?我是Android工作室的新手。我想在我的應用程序中找到距離。目標地址由mlatmlong定義,當前地址通過GPS值計算,其值在經度和緯度。如何查找兩個經度位置之間的距離?

我的代碼是:

double mlat =30.726030; 
    double mlong=76.759268; 
    imgButton1 =(ImageButton)findViewById(R.id.imageButton1); 
    mgButton1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Location gpsLocation = appLocationService.getLocation(LocationManager.GPS_PROVIDER); 
       if (gpsLocation != null) { 
        double latitude = gpsLocation.getLatitude(); 
        double longitude = gpsLocation.getLongitude();  
        String result = "Latitude: " + gpsLocation.getLatitude() + 
            " Longitude: " + gpsLocation.getLongitude(); 
        tvAddress.setText(result); 
       } else { 
        showSettingsAlert(); 
       } 
      } 
    }); 

告訴我如何找到兩個經度和緯度之間的距離。

回答

0

試試下面

Location loc1 = new Location(""); 
loc1.setLatitude(lat1); 
loc1.setLongitude(lon1); 

Location loc2 = new Location(""); 
loc2.setLatitude(lat2); 
loc2.setLongitude(lon2); 

float distanceInMeters = loc1.distanceTo(loc2); 
+0

thanx的用戶persious時間 –

0

這是簡單的座標運算。或者你可以用這個方法,我found by googling

0

使用distanceBetween方法來計算距離:

Location.distanceBetween(double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results) 
相關問題