我想在MapView上顯示我當前的位置。我向故事板添加了一個按鈕。MapView不會加載當前用戶位置,也不會居中/放大它
第一個問題:當前位置沒有顯示在我的屏幕上。
第二個問題:如果我點擊按鈕,MapView不會縮放到位置。
我ViewController.m: (按鈕方法名稱是:getLocation
)
#import "ViewController.h"
@import CoreLocation;
@interface ViewController() <CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager *locationManager;
@end
@implementation ViewController
@synthesize mapview = _mapview;
- (void)viewDidLoad {
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
}
mapview.showsUserLocation = YES;
[self.locationManager startUpdatingLocation];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)setMap:(id)sender
{
switch (((UISegmentedControl *) sender).selectedSegmentIndex) {
case 0:
mapview.mapType = MKMapTypeStandard;
break;
case 1:
mapview.mapType = MKMapTypeSatellite;
break;
case 2:
mapview.mapType = MKMapTypeHybrid;
break;
default:
break;
}
}
// The sender here are a Button on my Storyboard, who should show the current device Location if i click on it.
-(IBAction)getLocation:(id)sender
{
mapview.showsUserLocation = YES;
//NSLog(@"Koordinaten des Geräts: %@", mapview.userLocation.description);
//[mapview setCenterCoordinate:mapview.userLocation.coordinate animated:YES];
}
@end
你在iOS的8測試它? – 2014-11-21 13:39:28
你是否實現了CLLocationManagerDelegate方法? – 2014-11-21 13:43:16
是的,我還在info.plist中設置了密鑰 – Linus 2014-11-21 13:44:59