How to return points within lightning?

Asked

Viewed 1,854 times

2

I am developing 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’ve devised the following logic:

  • Get current user location
  • Insert the user’s location as the point A of the radius, and the distance of that radius as the point B, in latitude and longitude.

    But I’m having a hard time determining how to calculate and convert that distance to meters or kilometers.

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

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

    Does anyone know an API or methods that can help you craft this function.

    NOTE: I am using the API google maps iOS to show the map, I am storing the points of the places that will be searched within the radius in the App itself. I’m using Google Maps coordinates.

Image to illustrate: inserir a descrição da imagem aqui

  • When you say you have the points stored in the app you are referring to a comic book?

  • Yes, the locations are stored using coredata and include name and coordinates (latitude, longetude).

  • I had an idea to solve this problem, but it returned another question. Possible answer: Send the coordinates to Cclocation, and pick the distance, and make the following comparison: if(distancia < X){ // Dentro do Raio, slavo no array} But this concept will force the app to go through ALL stored points. To solve this problem then I thought of implementing something using a binary search tree (Correct me if I’m wrong with the name), but never implemented one before.

  • I suppose you can filter these data by latitude/longitude or not? Excuse the question my knowledge rises IOS/Objectivec is almost null.

  • Tranquil! : I believe you have heard of Binary Tree. If yes it would help a lot.

  • I’ll give you an answer see if it fits.

  • @ramaral thanks "brother"! By name we already have some degree of kinship kkkk Thank you.

Show 2 more comments

3 answers

3


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");

1

I did this in a project where I used the distance between two points ma taking into account the curvature of the earth, at small distances I see no problem, but at larger 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 the distance between two points in meters, so if you define a radius of 30KM is just to check this distance.

1

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

The Googlemaps api for android has a class with several methods that allow to make some calculations with points defined as Lat/Long allowing to calculate, namely, another point at a certain distance from the first second a 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 are left from the user’s location at a distance thunderbolt in directions (A)0º, (B)90º, (C)180º and (D)270º
For this use the function computeOffsetWithLatitude

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

P.Lat <= A.Lat && P.Lat >= C.Lat && P.Long <= B.Lon && P.Long >= D.Long

Browser other questions tagged

You are not signed in. Login or sign up in order to post.