2012-02-03 62 views
0

我讀過很多話題,但我不認爲我真的得到他們。如何在當前位置加載少量註釋?

我在其中一個主題中找到了下面的代碼,但我不知道如何放置它。我應該把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]; 

回答

0

可能要做的第一件事就是將數據集羣到地圖上。這意味着將幾個點合併到地圖上的一個點上。用戶放大得越多,聚類將被解析爲放置在地圖上的單個點。

有一些建議,如何做到這一點在這個崗位:

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

下一步就是通過你的數據itterate並檢查是否有一點是以防萬一用戶位置的區域內。您可以像這樣獲得可見顯示區域的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]; 
相關問題