1

I get the data from my database which contains, TITLE, SNIPPET and LOCATION and tried to test to check the distances between my currentlocation. I'm confused how to display the title of the closest marker to my position.

    List<MyMarkerObj> m = data.getMyMarkers();
                for (int i = 0; i < m.size(); i++) {
                    String[] slatlng =  m.get(i).getPosition().split(" ");
                    LatLng lat = new LatLng(Double.valueOf(slatlng[0]), Double.valueOf(slatlng[1]));
                    map.addMarker(new MarkerOptions()
                            .title(m.get(i).getTitle())
                            .snippet(m.get(i).getSnippet())
                            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
                            .position(lat)
                            );

                    float[] distance = new float[1];
                    Location.distanceBetween(currentlat, currentlong,Double.valueOf(slatlng[0]), Double.valueOf(slatlng[1]), distance);
                    Toast.makeText(getActivity(), "Marker Distance: "+ m.get(i).getTitle() +" "+distance[0], Toast.LENGTH_LONG).show();

                }

2 Answers2

5

I made few changes here. Try it..

List<MyMarkerObj> m = data.getMyMarkers();
float mindist;
int pos=0;
            for (int i = 0; i < m.size(); i++) {
                String[] slatlng =  m.get(i).getPosition().split(" ");
                LatLng lat = new LatLng(Double.valueOf(slatlng[0]), Double.valueOf(slatlng[1]));
                map.addMarker(new MarkerOptions()
                        .title(m.get(i).getTitle())
                        .snippet(m.get(i).getSnippet())
                        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
                        .position(lat)
                        );

                float[] distance = new float[1];
                Location.distanceBetween(currentlat, currentlong,Double.valueOf(slatlng[0]), Double.valueOf(slatlng[1]), distance);
                if(i==0) mindist=distance[0];
                else if(mindist>distance[0]) {
                   mindist=distance[0];
                   pos=i;
                 }
            }
  Toast.makeText(getActivity(), "Closest Marker Distance: "+ m.get(pos).getTitle() +" "+mindist, Toast.LENGTH_LONG).show();
Nizam
  • 5,698
  • 9
  • 45
  • 57
2

You can use this simple function to calculate distance between two points in latitude and longitude format it works like a charm, and you can then check for which distance is closest from your location. just pass the latitude and longitude of both location in it

public double distanceFrom(double lat1, double lng1, double lat2, double lng2) {
    double earthRadius = 3958.75;
    double dLat = Math.toRadians(lat2-lat1);
    double dLng = Math.toRadians(lng2-lng1);
    double a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.sin(dLng/2) * Math.sin(dLng/2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    double dist = earthRadius * c;
    int meterConversion = 1609;
    return new Double(dist * meterConversion).floatValue();    // this will return distance
}
Cyph3rCod3r
  • 1,978
  • 23
  • 33