【发布时间】:2014-02-01 08:10:12
【问题描述】:
MKMapView 有问题,在我将当前视图更改为另一个视图并再次返回地图后,它会一直重置。事实上,第一次一切顺利,地图正确地以用户位置为中心,但第二次就不行了。
这是我的代码:
MapViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface MapViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>
@property (retain, nonatomic) IBOutlet MKMapView *mapView;
@end
MapViewController.m
#import "MapViewController.h"
@interface MapViewController ()
@end
MKMapView *mapView;
@implementation MapViewController
@synthesize mapView;
- (void)mapView:(MKMapView *)aMapView didUpdateUserLocation:(MKUserLocation *)aUserLocation
{
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.009;
span.longitudeDelta = 0.009;
CLLocationCoordinate2D location;
location.latitude = aUserLocation.coordinate.latitude;
location.longitude = aUserLocation.coordinate.longitude;
region.span = span;
region.center = location;
[aMapView setRegion:region animated:YES];
}
- (void)viewDidLoad
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGSize result = [[UIScreen mainScreen] bounds].size;
if (result.height == 480)
{
mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 64, 320, 416)];
mapView.showsUserLocation = YES;
mapView.mapType = MKMapTypeHybrid;
mapView.delegate = self;
}
if (result.height == 568)
{
mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 64, 320, 504)];
mapView.showsUserLocation = YES;
mapView.mapType = MKMapTypeHybrid;
mapView.delegate = self;
}
}
[self.view addSubview:mapView];
[super viewDidLoad];
}
@end
还有一些图片
第一次:
第二次:
感谢大家帮我解决这个问题,祝你有美好的一天!
【问题讨论】:
-
好的,我知道了。你是在说缩放级别吗?
-
当您移出视图时,请使用 map.center 存储缩放级别和位置坐标。然后当您再次返回时,将地图中心和缩放级别设置为它。
-
创建一个不同的方法来更新区域,并将其添加到 MKCoordinateRegion 区域; MKCoordinateSpan 跨度; span.latitudeDelta = 0.009; span.longitudeDelta = 0.009; CLLocationCoordinate2D位置; location.latitude = self.yourmapview.userlocation.coordinate.latitude; location.longitude = self.yourmapview.userlocation.coordinate.longitude; region.span = 跨度; region.center = 位置; [self.yourMapView setRegion:region Animation:YES];在做完你已经在做的事情之后,在 viewdidload 中调用它。在 didUpdateUserLocation 中也调用相同的
-
1) 为什么在@implementation 上方的.m 文件中声明了另一个全局
mapView?你不需要它。 2)在.h中,您有一个标记为IBOutlet的mapView,因此您将其连接到xib中的地图视图,对吗?那么为什么要在 viewDidLoad 中创建并添加一个新的呢? 3) 这是all MapViewController.m 中的代码吗?您是否对 .m 文件中的 mapView 进行了任何操作? 4) 您如何准确地“将当前视图更改为另一个视图并返回”?你能显示那个代码吗? -
代理出口应该连接到底部的橙色圆圈图标(红色立方体左侧的图标)。然后注释掉整个 viewDidLoad 方法。
标签: ios xcode mkmapview mapkit core-location