[英]Calculate distance between two coordinates out of 2 lists
Hey guys i have a question. 嗨,我有一个问题。
I implemented Google Maps in my App and save the coordinates I get into an SQL Lite Database (I also have a Start and Stop button). 我在我的应用程序中实现了Google Maps,并将坐标保存到SQL Lite数据库中(我也有一个“开始”和“停止”按钮)。 Now I want to calculate the distance between the starting point and the end point.
现在,我要计算起点和终点之间的距离。 I get the coordinates out of two lists(one for Latidude and one for Longitude) and my problem now is how can I iterate through the list and get the coordinates out of there.
我从两个列表中获取坐标(一个用于Latidude,一个用于经度),现在我的问题是如何遍历列表并从中获取坐标。 I also need too read out 2 coordinates at once because I need the start and the end coordinates and this I need to do till the list is finished.
我还需要一次读出2个坐标,因为我需要开始和结束坐标,而且我需要这样做直到列表完成。
So I hope somebody knows a solution for this problem and here is my code: 因此,我希望有人知道该问题的解决方案,这是我的代码:
public double calculateKM() {
allRoute = (ArrayList<Route>) db.getAllRouteByID(drive_id);
for (Route route : allRoute) {
Log.d("Car: ", route.getDate());
lat.add(route.getLatitude());//here I get the coordiantes out of the database
lon.add(route.getLongitude());
for (int i = 0; i < lat.size(); i++) {//and this is where I cant find a solution.
String element = lat.get(i);
double value = Double.parseDouble(element);
}
calculateDistance(...);
}
}
private double calculateDistance(double fromLong, double fromLat,
double toLong, double toLat) {
double d2r = Math.PI / 180;
double dLong = (toLong - fromLong) * d2r;
double dLat = (toLat - fromLat) * d2r;
double a = Math.pow(Math.sin(dLat / 2.0), 2) + Math.cos(fromLat * d2r)
* Math.cos(toLat * d2r) * Math.pow(Math.sin(dLong / 2.0), 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double d = 6367000 * c;
return Math.round(d);
}
As said in the comments, I agree that you shouldn't separate lat&lon at all. 正如评论中所说,我同意您完全不应将经纬分开。 You could make a storage class for them both:
您可以为它们都创建一个存储类:
class Position{
public final double lat;
public final double lon;
public Position(double lat, double lon){
this.lat = lat;
this.lon = lon;
}
}
And store this in the list: 并将其存储在列表中:
List<Position> positions = new ArrayList<Position>();
positions.add( new Position( lat, lon) );
And iterate through it 并遍历它
for(int i=0; i<positions.size()-1; i++){
Position a = positions.get(i);
Position b = positions.get(i+1);
[.....]
}
If you'd like to stick with your double-list-thingy, you could iterate through them easily: 如果您想坚持使用双重清单,则可以轻松地遍历它们:
for(int i=0; i<lat.size(); i++){
String latStr = lat.get(i);
String lonStr = lon.get(i);
[....]
}
But again, please don't. 但同样,请不要。
// create a class for saving latitude and longitude under any package
private class Cordinate
{
double lati,longi;
public Cordinate(double la,louble lo)
{
lati=la;
longi=lo;
}
// set getters and setters for this two variables
public double getLati() {
return lati;
}
public void setLati(double lati) {
this.lati = lati;
}
public double getLongi() {
return longi;
}
public void setLongi(double longi) {
this.longi = longi;
}
}
//define array list for saving the latitude and longitude
List<Cordinate> cordinateList = new ArrayList<Cordinate>();
// then inside your functn
for (Route route : allRoute) {
Log.d("Car: ", route.getDate());
cordinateList.add(new Cordinate(route.getLattitude,rout.getLongitude));
for (int i = 0; i < lat.size(); i++) {//and this is where I cant find a solution.
String element = lat.get(i);
double value = Double.parseDouble(element);
}
// then call the method CalculateDistance () by passing variables
// You can use cordinate variables for setting the location object
// for ex: loc.setLatitude(cordinateList(i).getLati());
// loc.setLatitude(cordinateList(i).getLongi());
// method for find distance
CalculateDistance(Location loc1,Location dest)
{
double distance=DistanceCalculator.getDistance(loc, dest);
distance=Math.round(distance * 100.0) / 100.0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.