是否有可能使嵌入式MKMapView
旋轉以始終面向iPhone所面對的方向?基本上我想模仿我自己的應用程序上的地圖應用程序旋轉功能。使用指南針方向旋轉MapView
我看到iPhone SDK不公開該功能。但是,我想知道是否可以使用CGAffineTransformMakeRotate
旋轉整個視圖。會影響敲擊和縮放嗎?有沒有更好的辦法?
是否有可能使嵌入式MKMapView
旋轉以始終面向iPhone所面對的方向?基本上我想模仿我自己的應用程序上的地圖應用程序旋轉功能。使用指南針方向旋轉MapView
我看到iPhone SDK不公開該功能。但是,我想知道是否可以使用CGAffineTransformMakeRotate
旋轉整個視圖。會影響敲擊和縮放嗎?有沒有更好的辦法?
要旋轉的MapView但沒有註釋,你可以使用下面的代碼,以彌補地圖旋轉。
- (void)locationManager:(CLLocationManager *)manager
didUpdateHeading:(CLHeading *)newHeading
{
double rotation = newHeading.magneticHeading * 3.14159/180;
CGPoint anchorPoint = CGPointMake(0, -23); // The anchor point for your pin
[mapView setTransform:CGAffineTransformMakeRotation(-rotation)];
[[mapView annotations] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
MKAnnotationView * view = [mapView viewForAnnotation:obj];
[view setTransform:CGAffineTransformMakeRotation(rotation)];
[view setCenterOffset:CGPointApplyAffineTransform(anchorPoint, CGAffineTransformMakeRotation(rotation))];
}];
}
另一個sollution是使用已經在IOS 5被添加到的MKMapView的新方法。
- (void)setUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated;
如果您在3.0 iPhone應用程序編程指南下查看Device Support
,您會發現有關磁力計(又名指南針)的信息。一旦開始獲取航向信息(通過didUpdateHeading
方法),您應該能夠獲取指南針數據,然後使用它來計算適用於地圖視圖的正確的旋轉變換值。
不確定它是否處理縮放。在標準地圖應用程序中,我注意到,只要開始捏縮放,羅盤航向跟蹤就會停止。
另外,請記住,位置方向是以度爲單位,而變換旋轉角度以弧度爲單位。
我可以確認它工作正常。下面是我使用的代碼:
[mapView setTransform:CGAffineTransformMakeRotation(-1 * currentHeading.magneticHeading * 3.14159/180)];
mapView
是我的MKMapView實例
上面的代碼工作旋轉地圖視圖,但註釋怎麼樣?如何將變換應用於這些? – 2011-02-03 22:40:07
我已在下面發佈了針對您的問題的答案。 – thijsai 2012-03-27 14:12:19
如果使用navigationVontroller試試這個:
//For Left button
MKUserTrackingBarButtonItem *buttonItem = [[MKUserTrackingBarButtonItem
alloc]initWithMapView:_mapView];
self.navigationItem.leftBarButtonItem = buttonItem;
// For Right Button
MKUserTrackingBarButtonItem *buttonItem = [[MKUserTrackingBarButtonItem
alloc]initWithMapView:_mapView];
self.navigationItem.rightBarButtonItem = buttonItem;
在SWIFT 3.0簡單的解決方案。確保把線mapViewDidFinishLoadingMap
否則會忽略它
public func mapViewDidFinishLoadingMap(_ mapView: MKMapView)
{
mapView.setUserTrackingMode(.followWithHeading, animated: false)
}
如果您不想在地圖上的用戶位置中心,你可能會做這樣的事情
public func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading)
{
if(CLLocationCoordinate2DIsValid(mapView.centerCoordinate))
{
mapView.camera.heading = newHeading.trueHeading
}
}
如何在iOS6中引入此功能?我們可以將地圖北向導向嗎?我試圖定位地圖,但它是失敗的。 – Meera 2013-06-06 05:52:56