2016-03-29 91 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) 
相关问题