2014-04-21 38 views
16

我正在使用MKUserTrackingBarButtonItem按鈕來允許用戶自動跟蹤他們在地圖上的位置。問題是,當他們點擊這個按鈕時,它放大得太遠了。我希望它以指定的縮放級別(即跨度)開始。我怎樣才能做到這一點?如何在使用MKUserTrackingBarButtonItem時指定縮放級別?

當用戶點擊按鈕變爲MKUserTrackingModeFollow時,它似乎使用用戶上次手動更改(即在地圖上使用手勢)的相同縮放級別。試圖通過setRegionsetVisibleMapRect指定不同的縮放級別時,不會影響將模式更改爲MKUserTrackingModeFollow時將使用的縮放級別。

試圖設置override mapView:didChangeUserTrackingMode:來設置區域會導致模式重新更改爲MKUserTrackingModeNone。例如:

- (void)mapView:(MKMapView *)mapView didChangeUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated { 
    if (mode == MKUserTrackingModeFollow) { 
     CLLocationCoordinate2D center = mapView.userLocation.location.coordinate; 
     MKCoordinateSpan span = MKCoordinateSpanMake(0.002306, 0.001717); 
     [mapView setRegion:MKCoordinateRegionMake(center, span) animated:YES]; 
     // [mapView setUserTrackingMode:MKUserTrackingModeFollow animated:NO]; 
    } 
} 

如果我試圖設置區域後立即復位模式時,如果用戶是靜止的正常工作,而且還可以放大退了出來,如果用戶在移動。

最簡單的解決方案是,如果有方法通過發送我的跨度值來簡單地爲MKUserTraking指定像縮放級別之類的東西。但是,由於這似乎不存在,我還能做什麼?

+0

你是否找到了解決這個?我目前正在對付同樣的問題。 –

回答

7

我有同樣的問題,並使用不同的方法來解決它。您可以使用MapCamera功能代替該按鈕。

在每一個新的位置做到這一點:

MKMapCamera *newCamera = [MKMapCamera cameraLookingAtCenterCoordinate:[newLocation coordinate] 
fromEyeCoordinate:[oldLocation coordinate] 
eyeAltitude:2000]; 

[mapView setCamera:newCamera animated:TRUE]; 

並與eyeAltitude玩。

如果用戶手動放大或縮小,您可以從mapview.camera.altitude讀取海拔高度值,也不要在用戶手動使用地圖時更新相機。

+0

看起來像這是唯一的方法! – Rassam

0

根據蘋果文檔這裏

https://developer.apple.com/reference/mapkit/mkmapview/1616208-usertrackingmode

設置跟蹤模式進行後續或遵循標題使用會導致地圖視圖集中在該位置的地圖,並開始跟蹤用戶位置。如果地圖縮小,則地圖視圖會自動放大用戶的位置,從而有效更改當前的可見區域。

由於這個原因,改變區域不會影響你的可見區域。

- (void)mapView:(MKMapView *)mapView didChangeUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated { 
if (mode == MKUserTrackingModeFollow) { 
    CLLocationCoordinate2D center = mapView.userLocation.location.coordinate; 
    MKCoordinateSpan span = MKCoordinateSpanMake(0.002306, 0.001717); 
    [mapView setRegion:MKCoordinateRegionMake(center, span) animated:YES]; 
    // [mapView setUserTrackingMode:MKUserTrackingModeFollow animated:NO]; 
} 
} 

那麼你只需要改變didChangeUserTrackingMode中心座標,而不是在MKUserTrackingBarButtonItem的點擊改變整個區域

- (void)mapView:(MKMapView *)mapView didChangeUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated { 
if (mode == MKUserTrackingModeFollow) { 
    [self.mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES]; 
    } 
} 

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation { 
    [self.mapView setCenterCoordinate:mapViewuserLocation.location.coordinate animated:YES]; 
} 

改變縮放級別

CLLocationCoordinate2D center = mapView.userLocation.location.coordinate; 
MKCoordinateSpan span = MKCoordinateSpanMake(0.002306, 0.001717); 
[mapView setRegion:MKCoordinateRegionMake(center, span) animated:YES];