来自CLLocationDeCoordinate2D的MKCoordinate区域

时间:2014-08-11 11:28:43

标签: objective-c mapkit

我有以下对象的数组:

@interface House : NSObject
@property (nonatomic, assign) CLLocationCoordinate2D locationCoord2D;
@end

我正在尝试创建最小的MKCoordinateRegion,其中包含所有房屋。我怎样才能实现它?

从现在开始,我尝试使用valueForKeyPathMin以及Max谓词,但效果不佳。

1 个答案:

答案 0 :(得分:1)

尝试这样的事情;

1)像这样扩展你的House类,通过实例方法得到纬度和经度。

@implementation House

- (id)initWithLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude {

    if (self = [super init]) {

        _locationCoord2D = CLLocationCoordinate2DMake(latitude, longitude);
    }
    return self;
}

+ (instancetype)houseWithLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude {

    return [[self alloc] initWithLatitude:latitude longitude:longitude];
}

- (CLLocationDegrees)latitude { return _locationCoord2D.latitude; }

- (CLLocationDegrees)longitude { return _locationCoord2D.longitude; }

@end

注意:Instance methods for latitude and longitude对KVC很重要。

2)获取此类MKCoordinateRegion

CLLocationDegrees maxLatitude = [[houses valueForKeyPath:@"@max.latitude"] doubleValue];

CLLocationDegrees minLatitude = [[houses valueForKeyPath:@"@min.latitude"] doubleValue];

CLLocationDegrees maxLongitude = [[houses valueForKeyPath:@"@max.longitude"] doubleValue];

CLLocationDegrees minLongitude = [[houses valueForKeyPath:@"@min.longitude"] doubleValue];

MKCoordinateRegion coordinateRegion = MKCoordinateRegionMake(CLLocationCoordinate2DMake(minLatitude + (maxLatitude - minLatitude)/2, minLongitude + (maxLongitude - minLongitude)/2), MKCoordinateSpanMake(maxLatitude - minLatitude, maxLongitude - minLongitude));

这里的一切都很简单。为边距添加更多delta。多数民众赞成。