objective-c - startmonitoring - location when in use description
MapKit fornisce lat e lang errati-iOS 8 (3)
Quando provo a utilizzare la mappa in iOS 8, ottengo l'errore qui sotto.
Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.
Per questo ho provato la soluzione da qui , ma ancora senza fortuna.
Di seguito è quello che ho fatto.
Passaggio 1. Avere accesso a Info.plist
NSLocationWhenInUseUsageDescription - This is test text
Passaggio 2. Aggiornato - (CLLocationCoordinate2D) getLocation
-(CLLocationCoordinate2D) getLocation{
CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
// this is change
if(IS_OS_8_OR_LATER) {
[locationManager requestWhenInUseAuthorization];
}
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
return coordinate;
}
Passaggio 3. Prefix.pch
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
Passaggio 4. Eliminare di nuovo l'app con in viewDidLoad.
CLLocationCoordinate2D theCoordinate;
theCoordinate = [self getLocation];
NSLog(@"ffffff===%f===%f", theCoordinate.latitude, theCoordinate.longitude);
Ancora ottengo l'uscita come
ffffff===0.000===0.000
Nota:
Ricevo un avvertimento, ma è solo per mezzo secondo e scompare e poi ricevo il messaggio come
CLLocationCoordinate2D theCoordinate;
theCoordinate = [self getLocation];
NSLog(@"ffffff===%f===%f", theCoordinate.latitude, theCoordinate.longitude);
Quando vado in setting per controllare lo stato di Location, di seguito è quello che ho.
Qualche idea sul perché questo sta accadendo?
Sto testando su iPad 2 iOS 8.0 e simulatore iOS 8.0
È necessario implementare il delegato CLLocationManager (- (void) locationManager: (CLLocationManager *) manager didUpdateLocations: (NSArray *) posizioni) per ottenere gli aggiornamenti della posizione.
Ecco uno snippet di codice per il tuo riferimento:
-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *currentLocation = [locations lastObject];
NSLog(@"ffffff===%f===%f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
}
In alternativa, è possibile implementare il seguente metodo delegato di Mapkit:
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
MKUserLocation *myLocation = userLocation;
NSLog(@"ffffff===%f===%f", myLocation.coordinate.latitude, myLocation.coordinate.longitude);
}
La requestWhenInUseAuthorization
viene eseguita in modo asincrono, quindi non si dovrebbe chiamare startUpdatingLocation
finché non si ottiene una modifica dello stato di autorizzazione nel delegato.
Si! Ho la soluzione, Ecco il mio intero codice e le cose aggiunte per farlo funzionare. Un ringraziamento speciale a @MBarton per il suo grande aiuto. Grazie anche a @ Vinh Nguyen per aver investito il suo prezioso tempo nel risolvere il mio problema.
Aggiunto Core Location Framework in Target-> Generale-> Framework e librerie collegate
Aggiunto nel file .plist
NSLocationAlwaysUsageDescription
Vedi Screenshot:
Nel mio ViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <MapKit/MKAnnotation.h>
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
@interface ViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>
{
__weak IBOutlet UINavigationItem *navigationItem;
}
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property(nonatomic, retain) CLLocationManager *locationManager;
@end
Quindi in ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize mapView;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self setUpMap];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)setUpMap
{
mapView.delegate = self;
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
#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 requestAlwaysAuthorization];
}
#endif
[self.locationManager startUpdatingLocation];
mapView.showsUserLocation = YES;
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:YES];
self.locationManager.distanceFilter = kCLDistanceFilterNone;
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];
}
Ufff ...! Hai la soluzione dopo aver combattuto con molti codici dagli ultimi 5 giorni ...