find closest marker to my current location
<p>I have an arrayList of markers and I want to find the closest marker to my current location.
I had no idea how to find that marker so I searched and found same problem in here.</p>

<p><a href="https://stackoverflow.com/questions/4057665/google-maps-api-v3-find-nearest-markers">Google Maps Api v3 - find nearest markers</a></p>

<p>then I tried to convert those code to java but it doesn't work now.</p>

<p><code>closest</code> doesn't change and always it is -1.</p>

<p>Is there any better solution for this problem or I can make the following code usable?</p>

<pre><code>public void findNearMarker(){

    double pi = Math.PI;
    int R = 6371; //equatorial radius
    double[] distances = new double[2];
    double d = 0;
    int i;
    int closest = -1;

    for ( i = 0; i == markerArrayList.size(); i++){

        double lat2 = markerArrayList.get(i).getPosition().latitude;
        double lon2 = markerArrayList.get(i).getPosition().longitude;

        double chLat = lat2 - currentLocation.getLatitude();
        double chLon = lon2 - currentLocation.getLongitude();

        double dLat = chLat*(pi/180);
        double dLon = chLon*(pi/180);

        double rLat1 = currentLocation.getLatitude()*(pi/180);
        double rLat2 = lat2 * (pi/180);

        double a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.sin(dLon/2)
                * Math.sin(dLon /2) * Math.cos(rLat1) * Math.cos(rLat2);

        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

        d = R * c;

        distances[i] = d;
        if (closest == -1 || d&lt; distances[closest]){
            closest = i;
        }
    }

}
</code></pre>