我正在尝试在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
答案 0 :(得分:0)
您可能缺少info.plist文件中的 NSLocationWhenInUseUsageDescription ,并且需要iOS 8以上版本,您可以选择将其值保留为空。在过去,可以选择包括一个NSLocationUsageDescription&#39; key,一个字符串,向用户解释计划使用位置服务的应用程序。现在它被分成两个单独的键( NSLocationWhenInUseUsageDescription 和 NSLocationAlwaysUsageDescription ),现在是强制性的;如果您在没有相应密钥的情况下调用requestWhenInUseAuthorization或requestAlwaysAuthorization,则提示只是不会显示给用户。
请注意,如果您指定 requestAlwaysAuthorization 键,但您的应用始终没有使用定位器,最好是在后台,那么您可以在应用商店提交期间面临苹果的拒绝
答案 1 :(得分:0)
-(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];