ios - 在 iOS map 中更改 Pin 方向

标签 ios swift3 mkmapview core-location

  • swift 3.0
  • MKMAPVIEW
  • iOS

  • 注: - (集成 AppleMap,不适用于 GoogleMap)

    我做了以下事情:
  • 实现 map 并将自定义图像添加到用户位置注释
  • 本地图打开时,它会在正确的地方显示用户位置

  • 我的需求:

    When User Move into different direction staying at same place (or different place) the pin (at current location) should automatically point the direction in which user points.



    例如:如果船显示在用户位置位置并且它指向北方,但如果用户向西移动,那么船(用户位置引脚)也应该指向那个方向。

    尝试使用以下代码:
    //MARK:Change Direction Methods
    
        func angle(fromCoordinate first: CLLocationCoordinate2D, toCoordinate second: CLLocationCoordinate2D) -> Float {
            let deltaLongitude = second.longitude - first.longitude
            let deltaLatitude  = second.latitude - first.latitude
            let angle = (.pi * 0.5) - atan(deltaLatitude / deltaLongitude)
            if deltaLongitude > 0 {
                return Float(angle)
            }
            else if deltaLongitude < 0 {
                return Float(angle) + .pi
            }
            else if deltaLatitude < 0 {
                return .pi
            }
    
            return 0.0
        }
    
        //Animate direction of User Location
        func animateUserLocation() {
    
            //Old Coordinate (PLAT - Previous Lat , PLON - Previous Long) 
            let oldLocation = CLLocationCoordinate2D(latitude: UserDefaults.standard.value(forKey: "PLAT") as! CLLocationDegrees, longitude: UserDefaults.standard.value(forKey: "PLON") as! CLLocationDegrees)
    
            //New Coordinate (PLAT - Current Lat , PLON - Current Long)
            let newLocation = CLLocationCoordinate2D(latitude: UserDefaults.standard.value(forKey: "LAT") as! CLLocationDegrees, longitude: UserDefaults.standard.value(forKey: "LON") as! CLLocationDegrees)
    
            let getAngle = angle(fromCoordinate:oldLocation, toCoordinate:newLocation)
            var myAnnotation : RestaurantAnnotation?
            if annotationArray.count > 0{
                myAnnotation = annotationArray[0]
            }
            else {
                return
            }
    
            UIView.animate(withDuration: 2, animations: {() -> Void in
                myAnnotation?.coordinate = newLocation
                let annotationView = self.map.view(for: myAnnotation!)
                annotationView?.transform = CGAffineTransform(rotationAngle: CGFloat(getAngle))
            })
    
            //Save Previous lat long
            UserDefaults.standard.set(UserDefaults.standard.value(forKey: "LAT"), forKey: "PLAT")
            UserDefaults.standard.set(UserDefaults.standard.value(forKey: "LON"), forKey: "PLON")
            UserDefaults().synchronize()
        }
    

    调用 animateUserLocation来自 didUpdateLocation 的方法方法但没有运气。

    请分享您的建议我做错了什么。提前致谢。

    最佳答案

    了解iOS's位置概念完全有助于解决有关 AppleMap 的任何问题.

    要移动 map Pin 或 AnnotationView ,我们可以使用 CoreLocation输出与用户当前位置相关的数据的框架。

    import CoreLocation
    
    class ViewController: UIViewController, CLLocationManagerDelegate { 
      var locationManager:CLLocationManager! 
    
      override func viewDidLoad() {
        super.viewDidLoad() 
    
        locationManager  = CLLocationManager() 
        locationManager.delegate = self 
        locationManager.startUpdatingHeading() 
      } 
    
      func locationManager(manager: CLLocationManager!, didUpdateHeading heading: CLHeading!) {
        // This will print out the direction the device is heading
        println(heading.magneticHeading) } 
      }
    }
    

    在上面的示例中,“heading.magneticHeading”将输出一个值,表示设备指向的方向。
  • 0 表示北
  • 90 表示东
  • 180 表示南
  • 270 表示西
  • 介于
  • 之间的所有其他内容

    下一步是使用这些值并旋转您的 AnnotationViewPin因此。
    CGAffineTransformMakeRotation可以帮助解决这个问题。

    例如,如果您想旋转到 imageview 以指向东北,这将需要 45 度值,您的代码可能看起来像这样。
    float degrees = 45 
    imageView.transform = CGAffineTransformMakeRotation(degrees * M_PI/180)
    

    请注意 CGAffineTransformMakeRotation()需要一个弧度值,在上面的示例中,我们通过将度数乘以半圆数将度数转换为弧度。

    以下链接真的很有帮助:
  • https://stackoverflow.com/a/7634232/3400991

  • 终于解决了我的问题。希望这个完整的答案也对其他人有所帮助。

    关于ios - 在 iOS map 中更改 Pin 方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41198165/

    相关文章:

    ios - 为什么我的 Swift 3.0 解码器 decodeInteger 不能用于整数?

    iphone - 如何在iPhone SDK中的模拟器中获取用户当前位置?

    ios - 具有自动布局约束问题的 TableViewCell

    ios - UIPageViewController 空白屏幕

    iphone - iphone如何判断当前年份是否闰年

    PHP 在不同应用程序中返回异常数据

    ios - 来自 Apple 网站的日期示例代码不起作用?

    ios - 将 UITextField 转换为 Int 或 Double

    ios - Mapview显示错误的搜索结果

    swift - 在 MKSnapshot 图像中绘制带标题的 MKPointAnnotation