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). 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. 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.
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.