2017-03-01 11 views
0

私はランドマークの経度と緯度の配列を持っています。 ランドマークとユーザーの場所の距離を計算して、ユーザーの場所から半径10kmなどのランドマークを取得するにはどうすればいいですか?ユーザーの所在地に近い場所を取得する

答えて

1
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; 
+0

それが働いたおかげで:) – pavlos

0

現在の場所の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(); 
} 
関連する問題