如何在android中计算纬度和经度之间的距离?
我正试图计算如何在android中计算纬度和经度之间的距离?,android,gps,location,distance,latitude-longitude,Android,Gps,Location,Distance,Latitude Longitude,我正试图计算LatLng点之间的距离。计算两个坐标很容易。我必须计算两个以上的latlng之间的距离,并计算一组latlng之间的累积距离。我根据以下代码计算两点之间的距离 tvDistance=(TextView)findViewById(R.id.textView4); Location loc1=new Location(""); loc1.setLatitude(11.2805); loc1.setLongitude(77.5989); Location
LatLng
点之间的距离。计算两个坐标很容易。我必须计算两个以上的latlng
之间的距离,并计算一组latlng之间的累积距离。我根据以下代码计算两点之间的距离
tvDistance=(TextView)findViewById(R.id.textView4);
Location loc1=new Location("");
loc1.setLatitude(11.2805);
loc1.setLongitude(77.5989);
Location loc2=new Location("");
loc2.setLatitude(11.2801);
loc2.setLongitude(77.5976);
DecimalFormat format=new DecimalFormat("#.##");
double distanceInMeters = loc1.distanceTo(loc2)/1000;
tvDistance.setText(format.format(distanceInMeters) + " Km's");
例如,现在我有16个LatLng
点。首先是起点,最后是终点。我在ArrayList
中有LatLngs
。尝试了以下代码。但它导致了ArrayIndexOutOfBoundException
。
有人知道一个方法吗?请与我分享。谢谢
private void calculateDistance() {
for (int i=0;i<coordList.size();i++){
LatLng l1=coordList.get(i);
double lat1=l1.latitude;
double lng1=l1.longitude;
Location location1=new Location("");
location1.setLatitude(lat1);
location1.setLongitude(lng1);
LatLng l2=coordList.get(i+1);
double lat2=l2.latitude;
double lng2=l2.longitude;
Location location2=new Location("");
location2.setLatitude(lat2);
location2.setLongitude(lng2);
DecimalFormat format=new DecimalFormat("#.##");
double distance=location1.distanceTo(location2)/1000;
Toast.makeText(MainPage.this,format.format(distance) + " Km's",Toast.LENGTH_SHORT).show();
}
}
private void calculateInstance(){
对于(inti=0;i
LatLng l2=coordList.get(i+1);
导致异常。
乔恩这条线
LatLng l2=coordList.get(i+1);
导致异常。
jon您可以使用SphereCalutil.computeLength方法:
来源:您可以使用SphereCaltil.computeLength方法:
资料来源:->如果你有一组纬度和经度,那么计算距离很容易
ArrayList<RideRoutes> ride_route_list = new ArrayList<>();
//suppose "ride_route_list" contains collections of Latitudes and longitudes
String distance_covered_str;
double total_meters = 0.0;
for(int i = 0; i < (ride_route_list.size() - 1); i++){
double previous_latitude = Double.parseDouble(ride_route_list.get(i).getRout_latitude());
double previous_longitude = Double.parseDouble(ride_route_list.get(i).getRout_longitude());
double updated_latitude = Double.parseDouble(ride_route_list.get(i+1).getRout_latitude());
double updated_longitude = Double.parseDouble(ride_route_list.get(i+1).getRout_longitude());
Location start_latlng = new Location("location_previous");
start_latlng.setLatitude(previous_latitude);
start_latlng.setLongitude(previous_longitude);
Location end_latlng = new Location("location_updated");
end_latlng.setLatitude(updated_latitude);
end_latlng.setLongitude(updated_longitude);
total_meters += start_latlng.distanceTo(end_latlng);
}
double distance_covered_km = total_meters / 1000;
distance_covered_str = String.format(Locale.getDefault(), "%.2f", distance_covered_km);
->如果你有一组纬度和经度,那么计算距离很容易
ArrayList<RideRoutes> ride_route_list = new ArrayList<>();
//suppose "ride_route_list" contains collections of Latitudes and longitudes
String distance_covered_str;
double total_meters = 0.0;
for(int i = 0; i < (ride_route_list.size() - 1); i++){
double previous_latitude = Double.parseDouble(ride_route_list.get(i).getRout_latitude());
double previous_longitude = Double.parseDouble(ride_route_list.get(i).getRout_longitude());
double updated_latitude = Double.parseDouble(ride_route_list.get(i+1).getRout_latitude());
double updated_longitude = Double.parseDouble(ride_route_list.get(i+1).getRout_longitude());
Location start_latlng = new Location("location_previous");
start_latlng.setLatitude(previous_latitude);
start_latlng.setLongitude(previous_longitude);
Location end_latlng = new Location("location_updated");
end_latlng.setLatitude(updated_latitude);
end_latlng.setLongitude(updated_longitude);
total_meters += start_latlng.distanceTo(end_latlng);
}
double distance_covered_km = total_meters / 1000;
distance_covered_str = String.format(Locale.getDefault(), "%.2f", distance_covered_km);
这是一个有趣的查找循环。此行为(int i=0;iThat)创建异常,这是一个有趣的查找循环。此行为(int i=0;这是最有效和最精确的计算。不,它不是最精确的,但足够了。它使用哈弗斯公式。维森斯公式适用于椭球体,更精确,但需要更多的计算能力。这两种方法在哪里定义的arcHav(havDistance(lat1,lat2,lng1-lng2)).archHav和havDistance?这是最有效和最精确的计算。不,这不是最精确的,但足够了。它使用哈弗斯公式。维森斯公式适用于椭球体,更精确,但需要更多的计算能力。这两种方法在哪里定义了Archv(havDistance(lat1,lat2,lng1-lng2))archHav先生和havDistance先生?