【发布时间】:2012-02-03 08:21:22
【问题描述】:
我阅读了很多主题,但我认为我没有真正理解它们。
我在其中一个主题中找到了以下代码,但我不知道该怎么写。我应该放入 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];
【问题讨论】:
标签: ios xcode sqlite map annotations