2014-01-10 74 views
0

我使用MKUserTrackingModeFollowWithHeading,以便地圖跟隨用戶位置並旋轉。問題是縮放級別不堅持。這裏是我的代碼MKUserTrackingModeFollowWithHeading,zoom

-(void) viewWillAppear:(BOOL)animated { 
[super viewWillAppear:animated]; 
self.locMapView.delegate = self; 
[self.locMapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading animated:YES]; 

}

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

MKMapPoint pt = MKMapPointForCoordinate(_locMapView.userLocation.coordinate); 
double w = MKMapPointsPerMeterAtLatitude(_locMapView.userLocation.coordinate.latitude) * 500; 
MKMapRect zoomRect = MKMapRectMake(pt.x - w/2.0, pt.y - w/2.0, w, w); 

[_locMapView setVisibleMapRect:zoomRect animated:YES]; 

}

我試圖設置縮放級別viewDidLoad中也沒有多少運氣。

當UserTrackingMode設置爲MKUserTrackingModeFollowWithHeading時,如何使地圖粘到縮放級別?

+0

與您的問題無關,但您不需要計算MKMapRect以設置地圖顯示的內容。使用MKCoordinateRegionMakeWithDistance可以更容易地直接使用座標和米。它返回一個MKCoordinateRegion,你可以使用setRegion而不是調用setVisibleMapRect。 – Anna

+0

謝謝你指出。 – user3117208

回答

0

下面是我解決這個問題的技巧:在放大/縮小之前設置mapView.userTrackingMode = MKUserTrackingModeNone,然後設置= MKUserTrackingModeFollowWithHeading。

@property (assign, nonatomic) MKUserTrackingMode lastTrackingMode; 

- (IBAction)zoomInButtonPressed:(id)sender { 
    self.mapView.userTrackingMode = MKUserTrackingModeNone; 

    MKCoordinateRegion newRegion = MKCoordinateRegionMake(self.mapView.region.center,MKCoordinateSpanMake(self.mapView.region.span.latitudeDelta*0.5, self.mapView.region.span.longitudeDelta*0.5)); 
    [self.mapView setRegion:newRegion]; 

    self.mapView.userTrackingMode = self.lastTrackingMode; 
} 

- (IBAction)zoomOutButtonPressed:(id)sender { 
    self.mapView.userTrackingMode = MKUserTrackingModeNone; 

    // Zoom-out code goes here 

    self.mapView.userTrackingMode = self.lastTrackingMode; 
}