从两个坐标计算中点
Calculating midpoint from two coordinates
我在尝试找到两点之间的中点时遇到了一些问题。这是我得到两点之间最短距离的部分:
double distance = 0;
double minDistance = Double.MAX_VALUE;
convertedHotSpotGeomList = RetrieveHotSpotAsyncTask.convertedHotSpotGeom;
LatLng[] minPoints = new LatLng[2];
for(int i = 0; i < convertedHotSpotGeomList.size(); i++){
LatLng point1 = new LatLng(convertedHotSpotGeomList.get(i).getY(), convertedHotSpotGeomList.get(i).getX());
LatLng point2 = new LatLng(convertedHotSpotGeomList.get(++i).getY(), convertedHotSpotGeomList.get(++i).getX());
distance = calculateDistance(point1, point2);
if(distance < minDistance){
minDistance = distance;
minPoints[0] = point1;
minPoints[1] = point2;
}
}
// Finish all the comparison and draw the circles
if(minPoints[0]!=null && minPoints[1] !=null){
CircleOptions circleOptions = new CircleOptions()
.center(minPoints[0])
.radius(1000)
.fillColor(Color.argb(95, 178, 30, 37))
.strokeColor(Color.TRANSPARENT);
CircleOptions circleOptions1= new CircleOptions()
.center(minPoints[1])
.radius(1000)
.fillColor(Color.argb(95, 88, 130, 37))
.strokeColor(Color.TRANSPARENT);
googleBasemap.addCircle(circleOptions);
googleBasemap.addCircle(circleOptions1);
midPoint(minPoints[0].latitude,minPoints[0].longitude,minPoints[1].latitude,minPoints[1].longitude);
}
}
然后以我画的两个圆的圆心为中心,调用这个方法求中点:
private void midPoint(double lat1,double lon1,double lat2,double lon2){
double dLon = Math.toRadians(lon2 - lon1);
lat1 = Math.toRadians(lat1);
lat2 = Math.toRadians(lat2);
lon1 = Math.toRadians(lon1);
double Bx = Math.cos(lat2) * Math.cos(dLon);
double By = Math.cos(lat2) * Math.sin(dLon);
double lat3 = Math.atan2(Math.sin(lat1) + Math.sin(lat2), Math.sqrt((Math.cos(lat1) + Bx) * (Math.cos(lat1) + Bx) + By * By));
double lon3 = lon1 + Math.atan2(By, Math.cos(lat1) + Bx);
Log.i("LAT", String.valueOf(lat3));
Log.i("LON", String.valueOf(lon3));
CircleOptions circleOptions = new CircleOptions()
.center(new LatLng(lon3, lat3))
.radius(1000)
.fillColor(Color.argb(95, 178, 30, 137))
.strokeColor(Color.TRANSPARENT);
googleBasemap.addCircle(circleOptions);
}
在中点,我想在 google 地图上再画一个圆圈。但是,当我打印出中点的纬度和经度时,我得到了这个:
LAT 0.02512664 LON 1.811859
据我所知,google地图的坐标应该在103. , 1. .我想这是因为格式不同,这就是第三个圈子没有出来的原因。有什么想法吗?
提前致谢。
double lat3 = Math.atan2(Math.sin(lat1) +...
double lon3 = lon1 + Math.atan2(By, Math.cos(lat1) + Bx);
这个returns弧度。去学位:
double latdeg = Math.toDegrees(lat3);
double londeg = Math.toDegrees(lon3);
103.81187377279383
1.4396504253445948
我在尝试找到两点之间的中点时遇到了一些问题。这是我得到两点之间最短距离的部分:
double distance = 0;
double minDistance = Double.MAX_VALUE;
convertedHotSpotGeomList = RetrieveHotSpotAsyncTask.convertedHotSpotGeom;
LatLng[] minPoints = new LatLng[2];
for(int i = 0; i < convertedHotSpotGeomList.size(); i++){
LatLng point1 = new LatLng(convertedHotSpotGeomList.get(i).getY(), convertedHotSpotGeomList.get(i).getX());
LatLng point2 = new LatLng(convertedHotSpotGeomList.get(++i).getY(), convertedHotSpotGeomList.get(++i).getX());
distance = calculateDistance(point1, point2);
if(distance < minDistance){
minDistance = distance;
minPoints[0] = point1;
minPoints[1] = point2;
}
}
// Finish all the comparison and draw the circles
if(minPoints[0]!=null && minPoints[1] !=null){
CircleOptions circleOptions = new CircleOptions()
.center(minPoints[0])
.radius(1000)
.fillColor(Color.argb(95, 178, 30, 37))
.strokeColor(Color.TRANSPARENT);
CircleOptions circleOptions1= new CircleOptions()
.center(minPoints[1])
.radius(1000)
.fillColor(Color.argb(95, 88, 130, 37))
.strokeColor(Color.TRANSPARENT);
googleBasemap.addCircle(circleOptions);
googleBasemap.addCircle(circleOptions1);
midPoint(minPoints[0].latitude,minPoints[0].longitude,minPoints[1].latitude,minPoints[1].longitude);
}
}
然后以我画的两个圆的圆心为中心,调用这个方法求中点:
private void midPoint(double lat1,double lon1,double lat2,double lon2){
double dLon = Math.toRadians(lon2 - lon1);
lat1 = Math.toRadians(lat1);
lat2 = Math.toRadians(lat2);
lon1 = Math.toRadians(lon1);
double Bx = Math.cos(lat2) * Math.cos(dLon);
double By = Math.cos(lat2) * Math.sin(dLon);
double lat3 = Math.atan2(Math.sin(lat1) + Math.sin(lat2), Math.sqrt((Math.cos(lat1) + Bx) * (Math.cos(lat1) + Bx) + By * By));
double lon3 = lon1 + Math.atan2(By, Math.cos(lat1) + Bx);
Log.i("LAT", String.valueOf(lat3));
Log.i("LON", String.valueOf(lon3));
CircleOptions circleOptions = new CircleOptions()
.center(new LatLng(lon3, lat3))
.radius(1000)
.fillColor(Color.argb(95, 178, 30, 137))
.strokeColor(Color.TRANSPARENT);
googleBasemap.addCircle(circleOptions);
}
在中点,我想在 google 地图上再画一个圆圈。但是,当我打印出中点的纬度和经度时,我得到了这个:
LAT 0.02512664 LON 1.811859
据我所知,google地图的坐标应该在103. , 1. .我想这是因为格式不同,这就是第三个圈子没有出来的原因。有什么想法吗?
提前致谢。
double lat3 = Math.atan2(Math.sin(lat1) +...
double lon3 = lon1 + Math.atan2(By, Math.cos(lat1) + Bx);
这个returns弧度。去学位:
double latdeg = Math.toDegrees(lat3);
double londeg = Math.toDegrees(lon3);
103.81187377279383
1.4396504253445948