尝试查找最小距离时遇到问题,然后从最小距离中绘制两个圆到地图上。
double distance = 0;
double minDistance;
convertedHotSpotGeomList = RetrieveHotSpotAsyncTask.convertedHotSpotGeom;
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 = calculateHotSpot(point1, point2);
Log.i("DIST", String.valueOf(distance));
minDistance = distance;
if(minDistance < distance){
minDistance = distance;
Log.i("MIN", String.valueOf(minDistance));
CircleOptions circleOptions = new CircleOptions()
.center(point1)
.radius(1000)
.fillColor(Color.argb(95, 178, 30, 37))
.strokeColor(Color.TRANSPARENT);
googleBasemap.addCircle(circleOptions);
CircleOptions circleOptions1= new CircleOptions()
.center(point2)
.radius(1000)
.fillColor(Color.argb(95, 88, 130, 37))
.strokeColor(Color.TRANSPARENT);
googleBasemap.addCircle(circleOptions1);
}
}
目前,我的DIST为15、3、15、14,但由于未打印出minDist,所以无法获取它。有任何想法吗?
提前致谢。
最佳答案
您可能想要:
distance = calculateHotSpot(point1, point2);
....
if(distance < minDistance){
minDistance = distance;
并将
minDistance
初始化为Double.MAX_VALUE
:double minDistance = Double.MAX_VALUE
最初的几行没有多大意义,因为
minDistance < distance
因为您只设置了minDistance = distance;
,所以永远不会得出true。 distance = calculateHotSpot(point1, point2);
....
minDistance = distance;
if(minDistance < distance){ // will never happen
minDistance = distance;
更新:
上面提到的问题不是唯一的问题,而是更完整的解决方案:
double minDistance = Double.MAX_VALUE;
convertedHotSpotGeomList = RetrieveHotSpotAsyncTask.convertedHotSpotGeom;
LatLng[] minPoints = new LatLng[2]; // save the minimum points
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 = calculateHotSpot(point1, point2);
Log.i("DIST", String.valueOf(distance));
if(distance < minDistance){
minDistance = distance;
Log.i("MIN", String.valueOf(minDistance));
minPoints[0] = point1;
minPoints[1] = point2;
}
}
// we finish all the comparison, so we draw the circles now
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);
}