Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c MapKit中最近注释的列表_Objective C_Xcode_Ios6_Mapkit - Fatal编程技术网

Objective c MapKit中最近注释的列表

Objective c MapKit中最近注释的列表,objective-c,xcode,ios6,mapkit,Objective C,Xcode,Ios6,Mapkit,我正在开发一个在地图上添加注释的iPhone应用程序,我已经完成了所有注释的插入。我用于所有注释(x100)的代码是: 我想知道的是,如何在一个列表中获得所有这些位置,以显示与用户位置最近的注释?您需要做的是计算用户和注释之间的距离 首先,在MyAnnotation中添加一个保持距离值的变量: 将以下内容添加到MyAnnotation.h: @property (nonatomic, assign) CLLocationDistance distance; 当然,也可以将其合成为.m文件。 其

我正在开发一个在地图上添加注释的iPhone应用程序,我已经完成了所有注释的插入。我用于所有注释(x100)的代码是:


我想知道的是,如何在一个列表中获得所有这些位置,以显示与用户位置最近的注释?

您需要做的是计算用户和注释之间的距离

首先,在
MyAnnotation
中添加一个保持距离值的变量: 将以下内容添加到
MyAnnotation.h

@property (nonatomic, assign) CLLocationDistance distance;
当然,也可以将其合成为.m文件。 其次,在mapView类(保留注释ect的类)中,在收到新位置时添加以下代码:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    [...]
    for (MyAnnotation *annotation in self.mapView.annotations) {
        CLLocationCoordinate2D coord = [annotation coordinate];
        CLLocation *anotLocation = [[CLLocation alloc] initWithLatitude:coord.latitude longitude:coord.longitude];
        annotation.distance = [newLocation distanceFromLocation:anotLocation];
    }

    NSArray *sortedArray;
    sortedArray = [self.mapView.annotations sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
        NSNumber *first = [NSNumber numberWithDouble:[(MyAnnotation*)a distance]];
        NSNumber *second = [NSNumber numberWithDouble:[(MyAnnotation*)b distance]];
        return [first compare:second];
    }];

    [...]
}
现在,您可以使用
sortedArray
作为tableView ect的源,该ect根据最近距离到最长距离的距离进行排序。
当然

谢谢!如您在第二段代码中所述,接收用户位置的最佳方式是什么?使用。我为它添加了委托方法。但是,我建议您重新操作计算代码,因为委托方法经常返回。也许你不想重新计算用户移动的每一厘米。还不太清楚:/I我是Obj-C的初学者,如果可以的话,你能给我发送用户位置的代码吗?谢谢你的帮助!好的,谢谢你的帮助!最后一个问题,你知道苹果使用大量注释(200-1000)会不会有问题?或者只要我把它们组合在一起就可以了我不能说。只有一种方法可以找到答案,对吗?我不能想象他们有问题。
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    [...]
    for (MyAnnotation *annotation in self.mapView.annotations) {
        CLLocationCoordinate2D coord = [annotation coordinate];
        CLLocation *anotLocation = [[CLLocation alloc] initWithLatitude:coord.latitude longitude:coord.longitude];
        annotation.distance = [newLocation distanceFromLocation:anotLocation];
    }

    NSArray *sortedArray;
    sortedArray = [self.mapView.annotations sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
        NSNumber *first = [NSNumber numberWithDouble:[(MyAnnotation*)a distance]];
        NSNumber *second = [NSNumber numberWithDouble:[(MyAnnotation*)b distance]];
        return [first compare:second];
    }];

    [...]
}