Objective c 来自CLLocationDecoordination2D的MK坐标区域
我有以下对象的数组:Objective c 来自CLLocationDecoordination2D的MK坐标区域,objective-c,mapkit,Objective C,Mapkit,我有以下对象的数组: @interface House : NSObject @property (nonatomic, assign) CLLocationCoordinate2D locationCoord2D; @end 我正在尝试创建最小的MKCoordinateRegion,它将包含其中的所有房屋。我怎样才能做到呢 从现在起,我尝试使用valueForKeyPath和Min和Max谓词,但没有得到好的结果 1) 像这样扩展House类,通过实例方法获得纬度和经度 @implement
@interface House : NSObject
@property (nonatomic, assign) CLLocationCoordinate2D locationCoord2D;
@end
我正在尝试创建最小的MKCoordinateRegion
,它将包含其中的所有房屋。我怎样才能做到呢
从现在起,我尝试使用
valueForKeyPath
和Min
和Max
谓词,但没有得到好的结果
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
注意:纬度和经度的实例方法对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));
这里一切都很简单。为边距添加更多的增量。就这样。这样你就可以在屏幕上同时显示所有的房子了吗?如果房屋是注释对象,则可以在iOS 7中调用showAnnotations。否则,请使用MKMapRectUnion()构建MKMapRect(然后设置visibleMapRect或mkcoordinaeregionformaprect())。但稍后我需要做更复杂的选项,如:缩放MKCoordinateRegion,比较是否一个接触另一个,等等。因此,使用showAnnotations对我来说是不够的。因此MKMapRectUnion应该可以帮助您。然后您可以使用其他函数,如MKMapRectIntersectsRect等。对于缩放,使用MKMapPoints可能比使用CLLoc更好ationCoordination2ds(仅在计算过程中)。@Anna谢谢你的建议。我会考虑的。