2012-03-30 7 views
1

私はアプリケーションのフロントページにマップビューを表示していますが、CLLocationを使用して現在の位置から領域と座標を設定しています。 アプリケーションを実行すると、青い画面がマップビューに表示されます。子ビューを開いてメイン画面に戻ると、マップは場所とズームレベルを完全に示します。 ブルースクリーンの原因となる次のコードに問題がありますか?ズームでの問題現在の位置を使用したMapView

- (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];  
} 

答えて

4

海の真ん中にある場所が(0、0)なので青色が表示されていると思います。おそらく初めてのため、CLLocationManagerの場所はまだ設定されていません。

ロケーションがまだあるかどうかを確認し、そうでなければCLLocationManagerDelegateメソッドlocationManager:didUpdateToLocation:fromLocation:でコールバックを待ってから、必要な場所に地図の中心を合わせます。

このような何か:

... 
@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]; 
    } 
} 

しかし、また、あなたはMKMapViewshowsUserLocationを見てみることをお勧めします。

+0

このデリゲートメソッドを実装する方法を教えてください。 – Firdous

+0

@Firdous - 例を追加しました。 – mattjgalloway

+0

my locationManager:didUpdateToLocation:fromLocation:viewDidLoadでmapView.delegate = selfを設定しても、doesntが呼び出されます。青い画面が表示されず、問題が解決したように見えますが、このデリゲートメソッドのログにnslogメッセージが表示されません – Firdous