2012-03-30 8 views
1

Ich zeige eine Kartenansicht auf der Startseite meiner Anwendung an, ich stelle die Region und die Koordinaten vom aktuellen Standort mit CLLocation ein. Wenn die Anwendung ausgeführt wird, wird ein blauer Bildschirm in der Kartenansicht angezeigt. Wenn ich die untergeordneten Ansichten öffne und zum Hauptbildschirm zurückkehre, wird die Karte perfekt mit dem Standort und der Zoomstufe angezeigt. Ist im folgenden Code etwas falsch, das den blauen Bildschirm verursacht?Problem beim Zoomen von MapView unter Verwendung des aktuellen Standorts

- (void)viewWillAppear:(BOOL)animated { 
    CLLocationManager *lm = [[CLLocationManager alloc] init]; 
    lm.delegate = self; 
    lm.desiredAccuracy = kCLLocationAccuracyBest; 
    lm.distanceFilter = kCLDistanceFilterNone; 
    [lm startUpdatingLocation]; 

    CLLocation *location = [lm location]; 

    CLLocationCoordinate2D coord; 


    coord = [location coordinate]; 

    CLLocationCoordinate2D zoomLocation; 
    zoomLocation.latitude = coord.latitude; //39.281516; 
    zoomLocation.longitude= coord.longitude; //-76.580806; 

    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.3*METERS_PER_MILE, 0.3*METERS_PER_MILE); 

    MKCoordinateRegion adjustedRegion = [self.mainMapView regionThatFits:viewRegion];     

    [self.mainMapView setRegion:adjustedRegion animated:YES];  
} 

Antwort

4

Meine Vermutung wäre, dass Sie blau sehen, denn die Lage ist (0, 0), die in der Mitte des Meeres. Wahrscheinlich, weil das erste Mal die Position auf der CLLocationManager noch nicht festgelegt ist.

Sie sollten wahrscheinlich prüfen, ob es noch einen Standort gibt, und wenn nicht, dann warten Sie auf einen Rückruf in der CLLocationManagerDelegate Methode locationManager:didUpdateToLocation:fromLocation: und zentrieren Sie dann die Karte an dem gewünschten Ort.

Etwas wie folgt aus:

... 
@property (nonatomic, assign) BOOL needsCentering 
... 

@synthesize needsCentering = _needsCentering; 

- (void)viewWillAppear:(BOOL)animated { 
    CLLocationManager *lm = [[CLLocationManager alloc] init]; 
    lm.delegate = self; 
    lm.desiredAccuracy = kCLLocationAccuracyBest; 
    lm.distanceFilter = kCLDistanceFilterNone; 
    [lm startUpdatingLocation]; 

    CLLocation *location = [lm location]; 

    if (!location) { 
     _needsCentering = YES; 
    } else { 
     _needsCentering = NO; 

     CLLocationCoordinate2D coord; 

     coord = [location coordinate]; 

     CLLocationCoordinate2D zoomLocation; 
     zoomLocation.latitude = coord.latitude; //39.281516; 
     zoomLocation.longitude= coord.longitude; //-76.580806; 

     MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.3*METERS_PER_MILE, 0.3*METERS_PER_MILE); 

     MKCoordinateRegion adjustedRegion = [self.mainMapView regionThatFits:viewRegion];     

     [self.mainMapView setRegion:adjustedRegion animated:YES]; 
    } 
} 

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 
    if (_needsCentering) { 
     _needsCentering = NO; 

     CLLocationCoordinate2D coord; 

     coord = [newLocation coordinate]; 

     CLLocationCoordinate2D zoomLocation; 
     zoomLocation.latitude = coord.latitude; //39.281516; 
     zoomLocation.longitude= coord.longitude; //-76.580806; 

     MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.3*METERS_PER_MILE, 0.3*METERS_PER_MILE); 

     MKCoordinateRegion adjustedRegion = [self.mainMapView regionThatFits:viewRegion];     

     [self.mainMapView setRegion:adjustedRegion animated:YES]; 
    } 
} 

Aber auch, möchten Sie vielleicht einen Blick auf showsUserLocation von MKMapView nehmen.

+0

schön, können Sie mir helfen, wie diese Delegiertenmethode zu implementieren – Firdous

+0

@Firdous - ein Beispiel hinzugefügt. – mattjgalloway

+0

mein locationManager: didUpdateToLocation: fromLocation: wird nicht aufgerufen, auch wenn ich die mapView.delegate = self in viewDidLoad gesetzt habe. Obwohl der blaue Bildschirm nicht erscheint und das Problem scheint gelöst, aber ich sehe nicht die Nslog-Nachricht im Protokoll von dieser Delegate-Methode – Firdous

Verwandte Themen