문제

I am working in android GoogleMap project. I have the Geo position of the user (latitude and longitude) of the user. I want to calculate the co-ordinates the region with in 5 km (circle) of the user so that I can filter out the places from the database around the user. Any suggestions?

도움이 되었습니까?

해결책

Latitude and Longitude are in degrees so you need to use a simple flat earth projection. IMPORTANT: all lat/lon values in the formula below should be in radians, not degrees! Using flat earth is adequate because your radius is 5km. The flat earth projection is:

var R = 6371; // km
var x = (lon2-lon1) * Math.cos((lat1+lat2)/2);
var y = (lat2-lat1);
var d = Math.sqrt(x*x + y*y) * R;

Now lat1/lon1 is the reference position, in your case, the position of the user. Lat2/lon2 would be the point your are testing. The variable 'd' is the distance. Since 'R' is specified in km, then 'd' will be in km.

If 'd' is less the 5 (ie, less than 5km), the point is inside the circle.

Reference: http://www.movable-type.co.uk/scripts/latlong.html

다른 팁

The first answer (TreyA) appears to be showing you a stripped down version of the Haversine function. Below is JS code I give people that implements the Haversine function and converts the decimal values to radians.

// Convert Degress to Radians
function Deg2Rad( deg ) {
   return deg * Math.PI / 180;
}

// Get Distance between two lat/lng points using the Haversine function
// First published by Roger Sinnott in Sky & Telescope magazine in 1984 (“Virtues of the Haversine”)
//
function Haversine(lat1,lon1,lat2,lon2)
{
    var R = 6372.8; // Earth Radius in Kilometers

    var dLat = Deg2Rad(lat2-lat1);  
    var dLon = Deg2Rad(lon2-lon1);  
    var a = Math.sin(dLat/2) * Math.sin(dLat/2) + 
                    Math.cos(Deg2Rad(lat1)) * Math.cos(Deg2Rad(lat2)) * 
                    Math.sin(dLon/2) * Math.sin(dLon/2);  
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    var d = R * c; 

    // Return Distance in Kilometers
    return d;
}

Here's a link to a direct download of the file: http://www.opengeocode.org/download/haversine/haversine.js

You can obtain the point (in coordinate) of the circumference with:

(x - x_o)^2 + (y - y_o)^2 = r^2

Where x_0 and y_0 are the coordinates of the user, r is the radius that you want to consider, and your points are x and y.

Because you have to compare the coordinates of the places with the coordinates of the circle, you can just put X and Y as the places coordinates and see if those solve the disequation:

(X - x_o)^2 + (Y - y_o)^2 <= r^2

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top