我在尝试找到最小距离和最小距离时遇到了一些问题,然后我将两个圆圈绘制到地图上。
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);
}
}
目前我得到的是15,3,15,14作为DIST,但由于没有打印出来,我无法获得minDist。有什么想法吗?
提前致谢。
答案 0 :(得分:2)
你可能想要:
distance = calculateHotSpot(point1, point2);
....
if(distance < minDistance){
minDistance = distance;
并将minDistance
初始化为Double.MAX_VALUE
:double minDistance = Double.MAX_VALUE
最初的几行没有多大意义,因为minDistance < distance
永远不会评估为真,因为你刚设置了minDistance = distance;
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);
}
答案 1 :(得分:2)
你的问题在于这两行彼此相邻:
minDistance = distance;
if(minDistance < distance){
首先,您将minDistance设置为distance的值。然后你要检查minDistance是否小于距离。由于永远不会小于您刚设置的值,因此该代码块永远无法评估。