【问题标题】:How to load only few annotations around current location?如何在当前位置仅加载少量注释?
【发布时间】: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


    【解决方案1】:

    可能要做的第一件事是将您的数据聚集在地图上。这意味着在地图上将几个点合并为一个点。用户放大的越多,聚类将被解析为地图上的单个点。

    在这篇文章中有一些建议:

    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];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-16
      相关资源
      最近更新 更多