我正在尝试创建一个函数,以便根据我所在的位置和其余点返回 map 上各个点之间的距离。
但是这样就可以得到数据库第一个点得到的距离。
你能帮我吗?
if (cursor.getCount() > 0) {
if (cursor != null && cursor.moveToFirst()) {
lat_s = >cursor.getString(cursor.getColumnIndex("latitude"));
lng_s = >cursor.getString(cursor.getColumnIndex("longitude"));
lat_p = Double.parseDouble(lat_s);
lng_p = Double.parseDouble(lng_s);
double dist = getDistancia(lat, lng, lat_p, lng_p);
}
@SuppressWarnings("deprecation")
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,
R.layout.list_row, cursor, from, to);
listView.setAdapter(mAdapter);
}
返回距离的函数是:
public double getDistancia(double latitude, double longitude,
double latitudePto, double longitudePto){
double dlon, dlat, a, distancia;
dlon = longitudePto - longitude;
dlat = latitudePto - latitude;
a = Math.pow(Math.sin(dlat/2),2) + Math.cos(latitude) *
Math.cos(latitudePto) * Math.pow(Math.sin(dlon/2),2);
distancia = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
return 6378140 * distancia; /* 6378140 is the radius of the Earth in meters*/
}
最佳答案
Google map 提供了用于获取两个位置之间距离的 API-
了解更多详情
https://developers.google.com/maps/documentation/directions/
你也可以使用这个-
Location locationA = new Location("point A");
locationA.setLatitude(mLatitude / 1E6);
locationA.setLongitude(mLongitude / 1E6);
Location locationB = new Location("point B");
locationB.setLatitude(lat / 1E6);
locationB.setLongitude(lng / 1E6);
double distance = locationA.distanceTo(locationB);
关于java - 点之间的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19356236/