0

I was having some problem when trying to find the minimum distance and from the minimum distance, I then plot two circles onto the map.

                    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);

                        }
                    }

Currently I am getting 15, 3, 15, 14 as DIST but I could not get the minDist as it is not printed out. Any ideas?

Thanks in advance.

2
  • I do not understand, you are looking for the minimum distance between any points on each circle? Maybe a math formula will give it. Commented Feb 2, 2015 at 15:50
  • Nope, because I have to calculate the distance between two points. Then for the minimum distance for two point, I get plot two circle on it and find the mid point of these two points Commented Feb 2, 2015 at 15:51

2 Answers 2

2

You probably want:

    distance = calculateHotSpot(point1, point2);
    ....
    if(distance < minDistance){  
        minDistance = distance;

and initialize minDistance to Double.MAX_VALUE: double minDistance = Double.MAX_VALUE

The original few lines does not make much sense, because minDistance < distance would never evaluates to true since you just set minDistance = distance;

    distance = calculateHotSpot(point1, point2);
    ....
    minDistance = distance;
    if(minDistance < distance){ // will never happen
        minDistance = distance;

Updated:

More complete solution as the point above is not the only issue:

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);
}
Sign up to request clarification or add additional context in comments.

12 Comments

So I declare my minDistance as 0 or?
@IWasSoLost Use Integer.MAX_VALUE, let me update in my answer
I see but how do I make sure that it's only the minDistance then I plot the circle? Because currently I am getting two sets of circles on the map. With the minDistance printed out as 15 and 3
@IWasSoLost Ok, I you have another issues in the code, let me include it in my post as well.
@IWasSoLost OK, please check it out the updated post
|
2

Your problem lies in these two lines next to each other:

                    minDistance = distance;
                    if(minDistance < distance){

So first you are setting minDistance to the value of distance. Then you are checking to see if minDistance is less than distance. Since it will never be less than the value you just set it to, that block of code will never evaluate.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.