Как загрузить только несколько аннотаций вокруг текущего местоположения? - PullRequest
0 голосов
/ 03 февраля 2012

Я прочитал много тем, но я не думаю, что я действительно получаю их.

Я нашел следующий код в одной из тем, но я не уверен, как его поставить. Должен ли я положить в viewdidload? или где-то еще?

И у меня есть большие данные аннотации 3000 точек. Но я не знаю, как читать данные sqlite (я использую данные plist) Пожалуйста, помогите мне.

Заранее спасибо.

// create and populate an array containing all potential annotations
NSMutableArray *allPotentialAnnotations = [NSMutableArray array];

for(all potential annotations)
{
    MyAnnotation *myannotation = [[MyAnnotation alloc]
                              initWithCoordinate:...whatever...];
    [allPotentialAnnotations addObject:myannotation];
    [myannotation release];
}

// set the user's current location as the reference location
[allPotentialAnnotations
 makeObjectsPerformSelector:@selector(setReferenceLocation:) 
 withObject:mapView.userLocation.location];

// sort the array based on distance from the reference location, by
// utilising the getter for 'distanceFromReferenceLocation' defined
// on each annotation (note that the factory method on NSSortDescriptor
// was introduced in iOS 4.0; use an explicit alloc, init, autorelease
// if you're aiming earlier)
NSSortDescriptor *sortDescriptor = 
[NSSortDescriptor
 sortDescriptorWithKey:@"distanceFromReferenceLocation" 
 ascending:YES];

[allPotentialAnnotations sortUsingDescriptors:
 [NSArray arrayWithObject:sortDescriptor]];

// remove extra annotations if there are more than five
if([allPotentialAnnotations count] > 5)
{
    [allPotentialAnnotations
     removeObjectsInRange:NSMakeRange(5, 
                                      [allPotentialAnnotations count] - 5)];
}

// and, finally, pass on to the MKMapView
[mapView addAnnotations:allPotentialAnnotations];   

1 Ответ

0 голосов
/ 03 августа 2012

, вероятно, первое, что нужно сделать, - это кластеризовать ваши данные на карте. это означает объединение нескольких точек в одну точку на карте. чем больше пользователь увеличивает масштаб, тем больше кластеров будет преобразовано в отдельные точки на карте.

Есть несколько рекомендаций, как это сделать в этом посте:

Как я могу уменьшить количество аннотаций на карте?

следующий шаг - пройти через ваши данные и проверить, находится ли одна точка в пределах области вокруг местоположения пользователя. Вы можете получить позицию GEO видимой области отображения следующим образом:

CLLocationCoordinate2d topLeft, bottomRight;
topLeft = [mapView convertPoint:CGPointMake(0, 0) toCoordinateFromView:mapView];
CGPoint pointBottomRight = CGPointMake(mapView.frame.size.width, mapView.frame.size.height);
bottomRight = [mapView convertPoint:pointBottomRight toCoordinateFromView:mapView];
...