晚上好,当用户按下 ViewController 上的条形按钮时,我遇到了加载 MapView 的应用程序的问题。MapViewController 被加载并且 Map 显示正确的注释(未在源代码中显示),但是第一次启动 App 并打开 MapView 时 startregion 是错误的。当我按下后退按钮一次并重新打开 MapView 时,startregion 很好。它只是第一次不起作用。
日志给了我从 plist 加载的正确值:47.572132 和 7.579397
自从我两周前开始编写 Objective-c 代码以来,请让你的答案尽可能简单;-)
h.文件:
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "DetailViewController.h"
#import "Annotation.h"
@interface MapViewController : UIViewController<CLLocationManagerDelegate> {
IBOutlet MKMapView *singlemapview;
}
@property (nonatomic, retain) NSArray *data;
@property int selectedBuilding;
@property (strong, nonatomic) CLLocationManager *location;
@property float longitude;
@property float latitude;
@end
m.文件:
#import "MapViewController.h"
@interface MapViewController ()
@end
@implementation MapViewController
@synthesize data;
@synthesize selectedBuilding;
@synthesize location, latitude, longitude;
- (void)viewDidLoad
{
[super viewDidLoad];
NSDictionary *dataItem = [data objectAtIndex:selectedBuilding];
latitude = [[dataItem objectForKey:@"Latitude"] floatValue];
longitude = [[dataItem objectForKey:@"Longitude"] floatValue];
NSLog (@"%f",latitude);
NSLog (@"%f",longitude);
MKCoordinateRegion startregion = { {0.0, 0.0}, {0.0, 0.0} };
startregion.center.latitude = latitude;
startregion.center.longitude = longitude;
startregion.span.latitudeDelta = 0.005;
startregion.span.longitudeDelta = 0.005;
[singlemapview setMapType:MKMapTypeSatellite];
[singlemapview setZoomEnabled:YES];
[singlemapview setScrollEnabled:YES];
[singlemapview addAnnotation:singlebuilding];
[singlemapview setRegion:startregion];
}