简体   繁体   中英

MapKit in iOS 8 not run

I am building an iOS application that display a POI in a MapView, but firstly I can display the map, and with iOS8 this is begin a problem. I read a lot question in this site, but none run on my app even if the code seems right.

I entered into myapplicationTest-info.plist the following code

<key>NSLocationAlwaysUsageDescription</key>

And the code in the file.m is this:

#import "MapViewController.h"

#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)

@interface MapViewController ()

@end

@implementation MapViewController
@synthesize mapView = _mapView ;


- (void)viewDidLoad
{

    [super viewDidLoad];

    self.mapView.delegate = self;
    self.locationManager.delegate = self;
    self.locationManager = [[CLLocationManager alloc] init];
if (IS_OS_8_OR_LATER) {
    //[self.locationManager requestWhenInUseAuthorization];
    [self.locationManager requestAlwaysAuthorization];
}

   [self.locationManager startUpdatingLocation];

   self.mapView.showsUserLocation = YES;
   [mapView setMapType:MKMapTypeStandard];
   [mapView setZoomEnabled:YES];
   [mapView setScrollEnabled:YES];
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:YES];

    self.locationManager.distanceFilter = kCLDistanceFilterNone; //Whenever we move
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [self.locationManager startUpdatingLocation];
    NSLog(@"%@", [self deviceLocation]);

    //View Area
    MKCoordinateRegion region = { { 0.0, 0.0 }, { 0.0, 0.0 } };
    region.center.latitude = self.locationManager.location.coordinate.latitude;
    region.center.longitude = self.locationManager.location.coordinate.longitude;
    region.span.longitudeDelta = 0.005f;
    region.span.longitudeDelta = 0.005f;
    [mapView setRegion:region animated:YES];

}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];

}
- (NSString *)deviceLocation {
    return [NSString stringWithFormat:@"latitude: %f longitude: %f", self.locationManager.location.coordinate.latitude, self.locationManager.location.coordinate.longitude];
}
- (NSString *)deviceLat {
    return [NSString stringWithFormat:@"%f", self.locationManager.location.coordinate.latitude];
}
- (NSString *)deviceLon {
    return [NSString stringWithFormat:@"%f", self.locationManager.location.coordinate.longitude];
}
- (NSString *)deviceAlt {
    return [NSString stringWithFormat:@"%f", self.locationManager.location.altitude];
}

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


@end

Where is the mistake?

Why it doesn't run?

You need to provide a value for NSLocationAlwaysUsageDescription key which is the string that describes why your app wants to use location services -

For example -

<key>NSLocationAlwaysUsageDescription</key>
<string>FindMeDonuts will use your location to identify nearby donut shops</string>

Also, rather than checking the iOS version, it is better to check if CLLocationManager responds to the authorisation selector -

if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
        [self.locationManager requestAlwaysAuthorization];
}

finally, this won't stop your map from updating, but it doesn't make sense - you are assigning a delegate to your CLLocationManager before you allocate and initialise it.

You should say -

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

I don't know if i understand your problem.

if your problem is about the map is not show, you need to set frame of your map.

self.mapView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

hope i could help you

The best way is to use the -locationManager:didUpdateLocations method provided by CLLocationManagerDelegate.

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 
{
    [self.locationManager stopUpdatingLocation];
    // Move this line here
    self.mapView.showsUserLocation = YES; 
}

After calling [self.locationManager startUpdatingLocation] in ViewDidLoad, you shouldn't set self.mapView.showsUserLocation to YES yet as permission to use location services is not yet approved. Use the delegate to wait/get a location update from the system before setting self.mapView.showsUserLocation to YES .

You also need to enable location services

if([CLLocationManager resolveClassMethod:@selector(locationServicesEnabled)]) {
    [CLLocationManager locationServicesEnabled];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM