计算两个坐标之间的距离从2列出了 [英] Calculate distance between two coordinates out of 2 lists
问题描述
我有一个问题。
我实现了谷歌地图在我的应用程序和保存我进入一个SQL数据库精简版的坐标(我也有一个开始和停止按钮)。现在我要计算的起点和终点之间的距离。我走出两个列表(一个用于Latidude,一个用于经度)和我的问题的坐标现在我怎么能遍历列表并获取坐标离开那里。我还需要一次,因为我需要开始和结束坐标也读出2个坐标,这我需要做的,直到列表完成。
所以,我希望有人知道这个问题的解决方案,这里是我的code:
公共双calculateKM(){
allRoute =(ArrayList的<路线与GT;)db.getAllRouteByID(drive_id);
对于(路线路线:allRoute){
Log.d(汽车,route.getDate());
lat.add(route.getLatitude()); //这里我得到coordiantes从数据库中
lon.add(route.getLongitude()); 的for(int i = 0; I< lat.size();我++){//这是我不能找到一个解决方案。
字符串元素= lat.get(I)
双值= Double.parseDouble(元); }
calculateDistance(...);
}
}私人双calculateDistance(双fromLong,双fromLat,
双toLong,双toLat){ 双D2R = Math.PI / 180;
双dLong =(toLong - fromLong)* D2R;
双DLAT =(toLat - fromLat)* D2R;
双一= Math.pow(Math.sin(DLAT / 2.0),2)+ Math.cos(fromLat * D2R)
* Math.cos(toLat * D2R)* Math.pow(Math.sin(dLong / 2.0),2);
双C = 2 * Math.atan2(的Math.sqrt(一)的Math.sqrt(1 - a)条);
双D = 6367000 * C;
返回Math.round(D); }
//创建任何包下保存的经度和纬度的一类
私有类Cordinate
{
双拉提,隆基; 公共Cordinate(双LA,louble LO)
{
拉提= LA;
隆基= LO;
}
//设置getter和setter这两个变量
公共双getLati(){
返回LATI;
}公共无效setLati(双LATI){
this.lati = LATI;
}公共双getLongi(){
返回隆基;
}公共无效setLongi(双隆基){
this.longi =隆基;
} }
//定义数组列表保存的纬度和经度
清单< Cordinate> cordinateList =新的ArrayList< Cordinate>();
//那么你的functn内
对于(路线路线:allRoute){
Log.d(汽车,route.getDate()); cordinateList.add(新Cordinate(route.getLattitude,rout.getLongitude));
的for(int i = 0; I< lat.size();我++){//这是我不能找到一个解决方案。
字符串元素= lat.get(I)
双值= Double.parseDouble(元);
}
//然后传递变量调用方法CalculateDistance()
//你可以使用cordinate变量设置位置对象
为前//:loc.setLatitude(cordinateList(ⅰ).getLati());
// loc.setLatitude(cordinateList(ⅰ).getLongi());
//用于查找距离法
CalculateDistance(位置LOC1,位置DEST)
{
双距离= DistanceCalculator.getDistance(LOC,DEST);
距离= Math.round(距离* 100.0)/ 100.0;
}
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);
}
// 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;
}
这篇关于计算两个坐标之间的距离从2列出了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!