使用指南针方向旋转 MapView

发布于 2024-07-30 00:14:35 字数 190 浏览 2 评论 0原文

是否可以让嵌入的 MKMapView 旋转以始终面向 iPhone 所面向的方向? 基本上我想在我自己的应用程序上模仿地图应用程序旋转功能。

我发现 iPhone SDK 没有公开该功能。 但是,我想知道使用 CGAffineTransformMakeRotate 旋转整个视图是否有效。 会影响点击和缩放吗? 有没有更好的办法?

Is it possible to have an embedded MKMapView rotate to always face the direction the iPhone is facing? Basically I want to mimic the Map app rotation feature on my own app.

I see that the iPhone SDK does not expose the functionality. However, I wonder if it would work to rotate the entire view using CGAffineTransformMakeRotate. Would it affect tapping and zooming? Is there a better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

能怎样 2024-08-06 00:14:38

如果你使用 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;

if you use a navigationVontroller try this:

//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;
〆凄凉。 2024-08-06 00:14:38

如果您查看设备支持下的 3.0 iPhone 应用程序编程指南,您将找到有关磁力计(又名指南针)的信息。 一旦开始获取标题信息(通过 didUpdateHeading 方法),您应该能够获取指南针数据,然后使用它来计算正确的旋转变换值以应用于地图视图。

不确定它是否可以处理缩放。 在标准地图应用程序中,我注意到一旦开始捏缩放,罗盘航向跟踪就会停止。

另外,请记住,位置方向以度为单位,而变换旋转角度以弧度为单位。

If you look in the 3.0 iPhone Application Programming Guide under Device Support you'll find information on the magnetometer (aka compass). Once you start getting heading information (through the didUpdateHeading method) you should be able to get the compass data then use that to calculate the proper rotation transform value to apply to the map view.

Not sure if it handles zooming. In the standard map application I've noticed that compass heading tracking stops as soon as you start pinch-zooming.

Also, keep in mind that location directions are in degrees whereas transform rotation angles are in radians.

夜深人未静 2024-08-06 00:14:37

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
    }
}

Simple solution in swift 3.0. Make sure to put the line in mapViewDidFinishLoadingMap or it will ignore it

public func mapViewDidFinishLoadingMap(_ mapView: MKMapView)
{
    mapView.setUserTrackingMode(.followWithHeading, animated: false)
}

If you don't want the map to center on the user location you might do something like this

public func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading)
{
    if(CLLocationCoordinate2DIsValid(mapView.centerCoordinate))
    {
        mapView.camera.heading = newHeading.trueHeading
    }
}
苄①跕圉湢 2024-08-06 00:14:37

我可以确认它工作正常。 这是我正在使用的代码:

[mapView setTransform:CGAffineTransformMakeRotation(-1 * currentHeading.magneticHeading * 3.14159 / 180)];

mapView 是我的 MKMapView 实例

I can confirm that it works fine. Here's the code that I'm using:

[mapView setTransform:CGAffineTransformMakeRotation(-1 * currentHeading.magneticHeading * 3.14159 / 180)];

mapView is my MKMapView instance

若无相欠,怎会相见 2024-08-06 00:14:36

要旋转地图视图而不旋转注释,您可以使用以下代码来补偿地图旋转。

- (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))];

  }];

}

另一个解决方案是使用 iOS 5 中添加到 MKMapView 的新方法。

看一下: http://developer.apple.com/ Library/ios/#documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html

- (void)setUserTrackingMode:(MKUserTrackingMode)模式动画:(BOOL)animated;

To rotate the mapView but not the annotations you could use the following code to compensate for the maps rotation.

- (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))];

  }];

}

Another sollution is using a new method that has been added in iOS 5 to MKMapView.

Take a look at: http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html

- (void)setUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文