问题
I was having some problem when trying to find the mid point between two point. Here is the part where I get the shortest distance between two point:
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);
}
}
And then from the center point of the two circles I drawn, I am calling this method to get the midpoint:
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);
}
At the midpoint, I wanted to draw another circle onto the google map. However, when I print out the lat and lng for the mid point, I am getting this:
LAT 0.02512664 LON 1.811859
From what I've known, the coordinate of google map should be in 103. , 1. . I guess it's because the format is different and that's why the third circle is not coming out. Any ideas?
Thanks in advance.
回答1:
double lat3 = Math.atan2(Math.sin(lat1) +...
double lon3 = lon1 + Math.atan2(By, Math.cos(lat1) + Bx);
This returns radians. Go to degrees:
double latdeg = Math.toDegrees(lat3);
double londeg = Math.toDegrees(lon3);
103.81187377279383
1.4396504253445948
来源:https://stackoverflow.com/questions/28282578/calculating-midpoint-from-two-coordinates