Вопрос

I have list a of places with coordinates
If users select particular location I want to suggest list of locations nearby using coordinates I can achieve that by putting loop and searching all the places within the distance from the following code

  for(i=0;i<locations.count;i++)
 CLLocation *locA = [[CLLocation alloc] initWithLatitude:lat1 longitude:long1];

 CLLocation *locB = [[CLLocation alloc] initWithLatitude:lat2 longitude:long2];

 CLLocationDistance distance = [locA distanceFromLocation:locB];
// if(distance<10)
 //{
 //show the pin
// }

But I guess It not a efficient way if we have more locations in the database What I can I do here

Is there any alternative way??

Это было полезно?

Решение

You should filter the content to reduce the amount you extract from the database. You need to define what 'nearby' means in your case, calculate the lat/long bounding box where everything outside that box is not 'nearby' and use the box min and max lat/long values to exclude things that don't match. This is done as part of the SQL query so it's very efficient.

Now, your question loop logic comes into play. The approach is fine, though using your own loop likely isn't best. Instead, look at using sortedArrayUsingComparator: or sortedArrayUsingFunction:context:.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top