How to return points within a radius?

1

I'm working on an app where I would need to return points (Lat, Long) in a radius of size X (Km) from the user's location (Lat, Long). I have come up with the following logic:

  • Get the user's current location
  • Enter the location of the user as the tip of the radius, and the distance of that radius as the point B, in latitude and longitude.

    But I'm struggling to figure out how to calculate and convert this distance to meters or kilometers.

    The points that will be searched are stored in the app, not requiring requests from a webservice.

    In the end I will list in an array the points that are within the radius.

    Someone knows about an API or methods that can help you build this function.

    I'm using the google maps iOS API to show the map, I'm storing the points of the locations that will be searched within the radius of the app itself. I'm using the coordinates of Google Maps.

Image to Illustrate:

    
asked by anonymous 11.04.2015 / 19:09

3 answers

2

If your problem is to determine if a pair of coordinates belongs to a circular region then I suggest using the following code, assuming a radius of 1km:

CLLocation *posicaoAtual = <Posicao retornada pelo CoreLocation>;

CLCircularRegion *areaCircular  = [[CLCircularRegion alloc] initWithCenter:posicaoAtual.coordinate radius:1000 identifier:@"MyID"];

CLLocation *outraPosicao = <Posicao que você quer testar se está dentro da região>;
if (areaCircular containsCoordinate:outraPosicao.coordinate])
    NSLog("Está dentro da região");
    
12.04.2015 / 19:38
1

What you can do instead of finding the points that are within a circle is to find the points that are within a frame that has that circle inscribed. A frame whose side is equal to twice the radius being in its center the location of the user.

The GoogleMaps api for android has a class with several methods that allow you to do some calculations with points defined as Lat / Long allowing you to calculate, in particular, another point at a certain distance from the first second in one direction.

I think this class does not exist in the API google maps iOS but found in GitHub An alternative.

The solution I suggest is to calculate the points that remain from the user's location radius in directions (A) 0º, (B) 90º, (C) 180º and (D) 270th
To do this use the function computeOffsetWithLatitude

The points (P) that are inside the square will be those whose Lat / Long make the following true expression:

P.Lat <= A.Lat && P.Lat >= C.Lat && P.Long <= B.Lon && P.Long >= D.Long
    
11.04.2015 / 20:59
1

I did this in a project where I used the distance between two ma points taking into account the curvature of the earth, at small distances I see no problem, but at longer distances this error is gradually greater.

 public double distaincia(double latInicial, double latFinal, double lngInicial, double lngFinal) {
    double distancia = 6378140 * Math.acos(Math.cos(Math.PI * (90 - latFinal) / 180) * Math.cos((90 - latInicial) * Math.PI / 180)
            + Math.sin((90 - latFinal) * Math.PI / 180) * Math.sin((90 - latInicial) * Math.PI / 180) * Math.cos((lngInicial - lngFinal) * Math.PI / 180));
    return distancia;
}

This returns me to the distance between two points in meters, so if you set a radius of 30KM just check this distance.

    
09.07.2015 / 20:19