我正在尝试在MapView上显示我当前的位置。我在情节提要中添加了一个按钮。
第一个问题:当前位置未显示在我的屏幕上。
第二个问题:如果我单击按钮,MapView 不会缩放到位置。
My 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
杂注标记 - CLLocationManagerDelegate Methods
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *newLocation = locations.lastObject;
}
- (void)locationManager: (CLLocationManager *)manager didFailWithError: (NSError *)error
{
}
对于缩放,请尝试此
@property (weak, nonatomic) IBOutlet MKMapView *mapAllLocations;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
mapAllLocations.ZoomEnabled = true;
mapAllLocations.ScrollEnabled = true;
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude =newLocation.latitude;
zoomLocation.longitude=newLocation.longitude;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation,1000.0, 1000.0);
[self.mapAllLocations setRegion:viewRegion animated:YES];
您的 info.plist 文件中可能缺少 NSLocationWhenInUseUsageDescription,并且需要 iOS 8 及更高版本,您可以选择将其值留空。过去,可以选择包括"NSLocationUsageDescription"键,这是一个向用户解释应用程序计划使用位置服务的字符串。现在它已被拆分为两个单独的键(NSLocationWhenInUseUsageDescription和NSLocationAlwaysUsageDescription),现在是强制性的;如果在没有相应键的情况下调用 requestWhenInUseAuthorization 或 requestAlwaysAuthorization,则不会向用户显示提示。
请记住,如果您指定了requestAlwaysAuthorization密钥,但您的应用程序没有始终使用定位器,最好是在后台使用,那么您可能会在应用商店提交期间面临苹果的拒绝。