簡體   English   中英

查找最接近我當前位置的標記

[英]find closest marker to my current location

我有一個arrayArray的標記,我想找到最接近當前位置的標記。 我不知道如何找到該標記,所以我在這里搜索並發現了相同的問題。

Google Maps Api v3-查找最近的標記

然后我嘗試將這些代碼轉換為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;
        }
    }

}

首先,您需要導入android sdk的location類

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];
    }

並獲得最近的標記,只需獲得標記中的第一項,加油即可:)

如果您遵循使用經度和緯度比較兩個位置

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM