2016-07-18 6 views
1

MapViewCoreMotionを使用してuserLocation点を使用して回転しようとしています。私はビューを回転させることに成功しましたが、1つの問題があります:mapViewが回転すると、白い背景が表示され始めました。(下の赤いボックスを無視)この写真に示すように:enter image description here
私はこれを達成するために使用しているコードは:Rotate MapView加速度計に基づいて

- (void)viewDidLoad { 
locationManager = [[CLLocationManager alloc] init]; 
    _mapView.delegate = self; 
    locationManager.delegate = self; 
[locationManager requestWhenInUseAuthorization]; 

    [locationManager startUpdatingLocation]; 

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

locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; 
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    locationManager.headingFilter = 1; 
    [locationManager startUpdatingHeading]; 

    motionManager = [[CMMotionManager alloc] init]; 
    motionManager.accelerometerUpdateInterval = 0.01; 
    motionManager.gyroUpdateInterval = 0.01; 

    [motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] 
             withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) { 
              if (!error) { 
               [self outputAccelertionData:accelerometerData.acceleration]; 
              } 
              else{ 
               NSLog(@"%@", error); 
              } 
             }]; 

} 

と見出し

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading { 

    //self.lblGrados.text = [NSString stringWithFormat:@"%.0f°", newHeading.magneticHeading]; 

    // Convert Degree to Radian and move the needle 
    float newRad = -newHeading.trueHeading * M_PI/180.0f; 

    [UIView animateWithDuration:0.6 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{ 
     self.mapView.transform = CGAffineTransformMakeRotation(newRad); 
    } completion:nil]; 
} 

ためのこの方法は、いずれかを呼び出します以下:最後に

- (void)outputAccelertionData:(CMAcceleration)acceleration{ 
    //UIInterfaceOrientation orientationNew; 

    // Get the current device angle 
    float xx = -acceleration.x; 
    float yy = acceleration.y; 
    float angle = atan2(yy, xx); 
} 

ANF:

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

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

ここで私は何が不足していますか?私が行った限り、それはself.mapView.transform = CGAffineTransformMakeRotation(newRad);と何か関係がありますが、私はそれを何に変えるべきか分かりません。

+1

の作品次のコード

[self.mapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading animated:true]; 

を試してみてください。 – sanman

+0

@サンマンはいこれは私が必要とするものです。この行は完璧に動作しますが、非常に速く動こうとすると、それを処理する方法があれば、 'EXE_BAD_ACCESS'と言うアプリがクラッシュします。私はこれをしました '[UIView animateWithDuration:0.6 delay:0 options:UIViewAnimationOptionCurveEaseInOutアニメーション:^ { //self.mapView.transform = CGAffineTransformMakeRotation(newRad); [self.mapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading animated:true]; }完了:なし]; ' –

+0

@sanman私は' didUpdateUserLocation'で '[self.mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];をコメントしましたが、もうクラッシュしません。 :) 手伝ってくれてどうもありがとう。 –

答えて

1

あなたはself.mapView.setUserTrackingMode(MKUserTrackingMode.FollowWithHeading、アニメーション:true)を使用することができ見出しでフォローしたい場合には

+0

説明にエラーが表示されずにアプリケーションがクラッシュすることがありますが、時には '__NSCFNumber isPitched'と時には' EXE_BAD_ACCESS'と表示されます –

+1

クラッシュの理由はわかりませんが、これが助けになるといいですねhttp://stackoverflow.com/questions/16617563/crash-on-setusertrackingmode-with-mkmapview-when-zoomed-in :) – sanman

関連する問題