我遇到问题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
这是一些照片
第一次:
第二次:
感谢大家帮我解决这个问题,祝你有个美好的一天!
答案 0 :(得分:1)
我查看了文档并找到了mapView属性 visibleMapRect
当您按下新视图时,将此值存储为类属性。
例如:
@property MkMapRect currentRect;
初始化currentRect = CGRectZero;
在推新视图之前,
currentRect = mapView.visibleMapRect;
当你以后来到这个视图时,将调用mapView委托并在其中一个委托方法中写下代码
if (currentRect.size.width!=0)
[mapView setVisibleMapRect:currentRect animated:YES].
它会起作用。
答案 1 :(得分:0)
创建一个不同的方法来更新区域
-(void)yourMethod{
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.009;
span.longitudeDelta = 0.009;
CLLocationCoordinate2D location;
location.latitude = self.yourmapview.userlocation.coordinate.latitude;
location.longitude = self.yourmapview.userlocation.coordinate.longitude;
region.span = span;
region.center = location;
[self.yourMapView setRegion:region animated:YES];
}
-(void)viewWillAppear{
//After [self.view addSubview:mapView];
[self yourMethod];
}
尝试一下,但现在不要更新“didUpdateUserLocation”。如果你在“viewWillAppear”方法中的“yourMapView.location”中有值,也请告诉我。