是否有可能使嵌入式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