我有以下问题:
我有一个“绘制的地图”(图像),我将其作为叠加层添加到 MapView 中。没问题..但我需要将 MapView 限制在覆盖区域,因此用户无法在该区域之外滚动/缩放..但应该可以在“边界”内滚动/缩放覆盖层的“ - 意味着我不能只禁用 MapView 的缩放/滚动。
关于这个主题有什么想法/解决方案吗?使用MapView/-Kit的原因是我需要向自定义地图添加各种POI。当仅使用 ImageView+ScrollView 来呈现自定义地图时,这可能会变得更加复杂。
我对这个主题进行了大量研究,但没有找到很好的解决方案。
任何帮助表示赞赏!
此致,
基督教
编辑:这是我们的解决方案:
您提供左上角和右下角坐标来限制地图。 (最小)缩放级别也受到限制。我已经停用了减速,您可以从地图上弹起一点(以获得更好的性能/用户体验)。我在叠加层中添加了约 1 公里的灰色边框,这样用户就无法看到谷歌的原始世界地图。
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
有限MapView.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
在尝试了有限 MKMapView 的不同方法之后,我得出的结论是,使用 mapDidChange 并在中心点超出边界时进行重置效果最好,并带有动画:是
这是我的做法(使用新西兰纬度/长跨度/中心)。
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
if ((mapView.region.span.latitudeDelta > 15.589921 ) || (mapView.region.span.longitudeDelta > 175.836914) ) {
CLLocationCoordinate2D centerCoord = CLLocationCoordinate2DMake(-41.162114, 172.836914);
MKCoordinateSpan spanOfNZ = MKCoordinateSpanMake(13.589921, 14.062500 );
MKCoordinateRegion NZRegion = MKCoordinateRegionMake(centerCoord, spanOfNZ);
[mapView setRegion: NZRegion animated: YES];
}
if (abs(abs(mapView.region.center.latitude) - 41.162114) > (13.589921 / 2) ) {
CLLocationCoordinate2D centerCoord = CLLocationCoordinate2DMake(-41.162114, 172.836914);
MKCoordinateSpan spanOfNZ = MKCoordinateSpanMake(13.589921, 14.062500 );
MKCoordinateRegion NZRegion = MKCoordinateRegionMake(centerCoord, spanOfNZ);
[mapView setRegion: NZRegion animated: YES];
}
if (abs(abs(mapView.region.center.longitude) - 172.836914) > (14.062500 / 2) ) {
CLLocationCoordinate2D centerCoord = CLLocationCoordinate2DMake(-41.162114, 172.836914);
MKCoordinateSpan spanOfNZ = MKCoordinateSpanMake(13.589921, 14.062500 );
MKCoordinateRegion NZRegion = MKCoordinateRegionMake(centerCoord, spanOfNZ);
[mapView setRegion: NZRegion animated: YES];
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)