如何在当前位置周围加载少量注释?

时间:2012-02-03 08:21:23

标签: ios xcode sqlite map annotations

我阅读了很多主题,但我认为我并没有真正得到它们。

我在其中一个主题中找到了以下代码,但我不知道该怎么做。我应该放入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 :(得分:0)

可能首先要做的是在地图上聚类数据。这意味着将几个点组合到地图上的一个点上。用户缩放的越多,群集将被解析为放置在地图上的单个点。

在这篇文章中有一些建议如何做到:

How can I reduce the number of annotations on a map?

下一步是检查数据并检查一个点是否位于用户位置周围的区域内。您可以像这样获得可见显示区域的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];