mapview注释无法在iphone 4s中运行

时间:2015-02-17 05:49:00

标签: ios objective-c iphone

我不知道我的代码有什么问题。此代码在iphone5到更高版本中工作正常。在第一次加载控制器时,地图将首先显示国家/地区,然后放大用户点和注释显示的位置。不幸的是,iphone4s的工作方式不同。它只显示国家,然后没有更多的点或注释显示。

这是我的代码:

#import "ViewController.h"
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
@interface ViewController ()
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (nonatomic,strong) CLLocationManager * locationManager;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.mapView.delegate = self;

    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;

#ifdef __IPHONE_8_0
    if(IS_OS_8_OR_LATER) {
        [self.locationManager requestWhenInUseAuthorization];
        [self.locationManager requestAlwaysAuthorization];
    }
#endif

    [self.locationManager startUpdatingLocation];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse) {
        self.mapView.showsUserLocation = YES;
    }
}


#pragma mark Map View
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{

    static NSString *identifier = @"getLocation";

    MKAnnotationView *annotationView = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    if (annotationView == nil) {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        annotationView.enabled = YES;
        annotationView.canShowCallout = YES;
    } else {
        annotationView.annotation = annotation;
    }

    return annotationView;
}

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{

    [self.mapView setShowsUserLocation:NO];
    [self.mapView removeAnnotations:self.mapView.annotations];

    CLLocation *currentLocation = [[CLLocation alloc] initWithLatitude:userLocation.coordinate.latitude longitude:userLocation.coordinate.longitude];
    CLGeocoder * geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemark, NSError *error) {

        NSString *annTitle = @"Address unknown";

        if (placemark.count > 0)
        {
            CLPlacemark *topResult = [placemark objectAtIndex:0];
            annTitle = [NSString stringWithFormat:@"%@", topResult.locality];
        }

        MKPointAnnotation *toAdd = [[MKPointAnnotation alloc]init];
        toAdd.coordinate = userLocation.coordinate;
        toAdd.title =annTitle;


        [self.mapView addAnnotation:toAdd];
    }];
}

-(void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views{
    MKAnnotationView *annotationView = [views objectAtIndex:0];
    id<MKAnnotation> mp = [annotationView annotation];

    [UIView animateWithDuration:3.0 animations:^{
        MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate] ,500,500);

        [self.mapView setRegion:region animated:YES];
    } completion:^(BOOL finished) {
        [self.mapView selectAnnotation:mp animated:YES];

    }];
}

和我的viewcontroller.m

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController <MKMapViewDelegate, MKAnnotation,CLLocationManagerDelegate>

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@end

但我收到此错误

Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.

错误只显示iphone4s。我的iphone4s的版本是ios8。

1 个答案:

答案 0 :(得分:0)

这种情况正在发生,因为你没有在plist中输入,如下所示。

enter image description here

如果仍然无效,请将代码更新到下面。

myMapView.delegate = self;
locationManager.delegate = self;
self.locationManager = [[CLLocationManager alloc] init];
#ifdef __IPHONE_8_0
if(IS_OS_8_OR_LATER) {
    // Use one or the other, not both. Depending on what you put in info.plist
    [self.locationManager requestWhenInUseAuthorization];
    //        [self.locationManager requestAlwaysAuthorization];
}
#endif
[self.locationManager startUpdatingLocation];

myMapView.showsUserLocation = YES;
[myMapView setMapType:MKMapTypeStandard];
[myMapView setZoomEnabled:YES];
[myMapView setScrollEnabled:YES];

编辑1

根据您的评论,您找不到info.plist。

您可以在支持文件中找到它。

enter image description here