找到距离我当前位置最近的标记

时间:2016-10-13 16:09:40

标签: java android google-maps

我有一个标记的数组列表,我想找到最接近我当前位置的标记。 我不知道如何找到那个标记,所以我在这里搜索并发现了同样的问题。

Google Maps Api v3 - find nearest markers

然后我尝试将这些代码转换为java,但它现在不起作用。

closest不会改变,并且始终为-1。

是否有更好的解决方案可以解决此问题,或者我可以使用以下代码?

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< distances[closest]){
            closest = i;
        }
    }

}

2 个答案:

答案 0 :(得分:1)

首先你需要导入android sdk的位置类

import android.location.Location;


 ArrayList<Marker> markers = new ArrayList<>();
markers = sortListbyDistance(markers, currentLocation.getLocation());


 public static ArrayList<Marker> sortListbyDistance(ArrayList<Marker> markers, final LatLng location){
        Collections.sort(markers, new Comparator<Marker>() {
            @Override
            public int compare(Marker marker2, Marker marker1) {
            //
                if(getDistanceBetweenPoints(marker1.getLocation(),location)>getDistanceBetweenPoints(marker2.getLocation(),location)){
                    return -1;
                } else {
                    return 1;
                }
            }
        });
        return markers;
    }


 public static float getDistanceBetweenPoints(double firstLatitude, double firstLongitude, double secondLatitude, double secondLongitude) {
        float[] results = new float[1];
        Location.distanceBetween(firstLatitude, firstLongitude, secondLatitude, secondLongitude, results);
        return results[0];
    }

并获得最近的标记只是获得标记中的第一项,欢呼:)

答案 1 :(得分:1)

如果你跟随 Comparing two locations using their Longitude and Latitude

/** calculates the distance between two locations in MILES */
private double distance(double lat1, double lng1, double lat2, double lng2) {

double earthRadius = 3958.75; // in miles, change to 6371 for kilometer output

double dLat = Math.toRadians(lat2-lat1);
double dLng = Math.toRadians(lng2-lng1);

double sindLat = Math.sin(dLat / 2);
double sindLng = Math.sin(dLng / 2);

double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2)
    * Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2));

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

double dist = earthRadius * c;

return dist; // output distance, in MILES
}

使用此函数循环遍历列表并获得最低的返回值。

您还可以使用Maps API

 Location locationA = new Location("point A");

locationA.setLatitude(latA);
locationA.setLongitude(lngA);

Location locationB = new Location("point B");

locationB.setLatitude(latB);
locationB.setLongitude(lngB);

float distance = locationA.distanceTo(locationB);