iOS-如何将MapView限制为特定区域?
|
我有以下问题:
我有一个“绘制的地图”(图像),我将其作为叠加层添加到MapView中。没问题..但是我需要将MapView限制为Overlay的区域,因此用户无法在该区域之外滚动/缩放。但是应该可以在\叠加层的“界限\”-表示我不能仅禁用MapView的缩放/滚动。
关于这个主题有什么想法/解决方案吗?使用MapView / -Kit的原因是我需要向自定义地图添加各种POI。仅使用ImageView + ScrollView呈现自定义地图时,这可能会变得更加复杂。
我对此主题进行了很多研究,但没有找到一个好的解决方案。
任何帮助表示赞赏!
最好的祝福,
基督教
编辑:这是我们的解决方案:
您提供左上和右下坐标以限制地图。 (最小)缩放级别也受到限制。我已停用减速功能,并且您可以从地图上稍微反弹一下(以获得更好的性能/辅助)。我在覆盖层上添加了约1公里的灰色边框,因此用户无法看到Google的原始世界地图。
LimitedMapView.h:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface LimitedMapView : MKMapView <UIScrollViewDelegate>{
}
@property (nonatomic, assign) CLLocationCoordinate2D topLeftCoordinate;
@property (nonatomic, assign) CLLocationCoordinate2D bottomRightCoordinate;
@end
LimitedMapView.m:
#import \"LimitedMapView.h\"
@implementation LimitedMapView
@synthesize topLeftCoordinate, bottomRightCoordinate;
- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
if([super respondsToSelector:@selector(scrollViewDidZoom:)]) [super scrollViewDidZoom:scrollView];
if ([self region].span.latitudeDelta > 0.002401f || [self region].span.longitudeDelta > 0.003433f) {
CLLocationCoordinate2D center = self.centerCoordinate;
MKCoordinateSpan span = MKCoordinateSpanMake(0.002401f, 0.003433f);
self.region = MKCoordinateRegionMake(center, span);
}
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
if([super respondsToSelector:@selector(scrollViewDidEndDragging:)]) [super scrollViewDidEndDragging:scrollView willDecelerate:decelerate];
MKCoordinateRegion currentRegion = self.region;
bool changeRegionLong = YES;
bool changeRegionLat = YES;
// LONGITUDE
if((currentRegion.center.longitude - (currentRegion.span.longitudeDelta/2)) < self.topLeftCoordinate.longitude) {
currentRegion.center.longitude = (topLeftCoordinate.longitude + (currentRegion.span.longitudeDelta/2));
} else if((currentRegion.center.longitude + (currentRegion.span.longitudeDelta/2)) > self.bottomRightCoordinate.longitude) {
currentRegion.center.longitude = (bottomRightCoordinate.longitude - (currentRegion.span.longitudeDelta/2));
} else {
changeRegionLong = NO;
}
// LATITUDE
if((currentRegion.center.latitude + (currentRegion.span.latitudeDelta/2)) > self.topLeftCoordinate.latitude) {
currentRegion.center.latitude = (topLeftCoordinate.latitude - (currentRegion.span.latitudeDelta/2));
} else if((currentRegion.center.latitude - (currentRegion.span.latitudeDelta/2)) < self.bottomRightCoordinate.latitude) {
currentRegion.center.latitude = (bottomRightCoordinate.latitude + (currentRegion.span.latitudeDelta/2));
} else {
changeRegionLat = NO;
}
if(changeRegionLong || changeRegionLat) [self setRegion:currentRegion animated:YES];
}
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
[scrollView setContentOffset:scrollView.contentOffset animated:YES];
}
@end
没有找到相关结果
已邀请:
3 个回复
柑恫祟
您应该能够将区域重置为您的特定区域。 查看该答案,以获取有关如何执行此操作的示例。 带来更精确的事件但更复杂的另一种解决方案(我目前正在成功地将其用于其他需求)是子类
并重写了
协议。 如果这样做,请确保这样调用
实现:
注意:虽然该协议是公共协议,但是
的实现方式未知,并且可能与iOS版本不同,因此在使用with8ѭ进行检查之前更安全。您必须调用超级实现,否则Map将无法正常工作。
嘘崇蔡对
凸晴