簡體   English   中英

在用戶位置附近獲取位置

[英]Getting locations near User's Location

我有一組經度和緯度的地標。 如何計算地標與用戶位置之間的距離,以獲取距離用戶位置半徑10公里的地標

如果您具有當前位置的LatLong點,則可以使用以下代碼計算到LatLongs之間的距離。

public float distance (float lat_a, float lng_a, float lat_b, float lng_b)
{
double earthRadius = 3958.75;
double latDiff = Math.toRadians(lat_b-lat_a);
double lngDiff = Math.toRadians(lng_b-lng_a);
double a = Math.sin(latDiff /2) * Math.sin(latDiff /2) +
Math.cos(Math.toRadians(lat_a)) * Math.cos(Math.toRadians(lat_b)) *
Math.sin(lngDiff /2) * Math.sin(lngDiff /2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double distance = earthRadius * c;

int meterConversion = 1609;

return new Float(distance * meterConversion).floatValue();
}
private static double distanceInKm(double lat1, double lon1, double lat2, double lon2) {
        int R = 6371; // km
        double x = (lon2 - lon1) * Math.cos((lat1 + lat2) / 2);
        double y = (lat2 - lat1);
        return (Math.sqrt(x * x + y * y) * R) / 1000;
    }

要么

Location location1 = new Location("");
location1.setLatitude(latitude1);
location1.setLongitude(longitude1);

Location location2 = new Location("");
location2.setLatitude(latitude2);
location2.setLongitude(longitude2);

float distanceInKm = (location1.distanceTo(location2))/1000;

暫無
暫無

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

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