MKMapView
iPhoneが向いている方向を常に向くように埋め込み回転させることは可能ですか?基本的に、自分のアプリでマップアプリの回転機能を模倣したいと思います。
iPhoneSDKが機能を公開していないようです。ただし、を使用してビュー全体を回転させると機能するのではないかと思いますCGAffineTransformMakeRotate
。タッピングとズームに影響しますか?もっと良い方法はありますか?
MKMapView
iPhoneが向いている方向を常に向くように埋め込み回転させることは可能ですか?基本的に、自分のアプリでマップアプリの回転機能を模倣したいと思います。
iPhoneSDKが機能を公開していないようです。ただし、を使用してビュー全体を回転させると機能するのではないかと思います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))];
}];
}
もう1つの解決策は、iOS5でMKMapViewに追加された新しいメソッドを使用することです。
ご覧ください:http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html
- (void)setUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated;
正常に動作していることを確認できます。これが私が使用しているコードです:
[mapView setTransform:CGAffineTransformMakeRotation(-1 * currentHeading.magneticHeading * 3.14159 / 180)];
mapView
私のMKMapViewインスタンスです
swift3.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
}
}
下の3.0iPhoneアプリケーションプログラミングガイドを見るとDevice Support
、磁力計(別名コンパス)に関する情報が見つかります。(メソッドを介して)見出し情報の取得を開始するdidUpdateHeading
と、コンパスデータを取得できるようになり、それを使用して、マップビューに適用する適切な回転変換値を計算できます。
ズームを処理できるかどうかわからない。標準の地図アプリケーションでは、ピンチズームを開始するとすぐにコンパスの機首方位の追跡が停止することに気づきました。
また、位置の方向は度であるのに対し、変換の回転角はラジアンであることに注意してください。
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;