【问题标题】:Change Pin direction in iOS Map在 iOS 地图中更改 Pin 方向
【发布时间】:2017-05-03 01:30:49
【问题描述】:
  • SWIFT 3.0
  • MKMAPVIEW
  • iOS

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

我做了以下事情:

  1. 实现地图并将自定义图像添加到用户位置注释
  2. 当地图打开时,它会在正确的地方显示用户位置

我的要求:

当用户移动到不同的方向并停留在同一个地方(或 不同的地方)引脚(在当前位置)应该自动 指向用户指向的方向。

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

尝试使用以下代码:

//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()
    }

didUpdateLocation 方法调用animateUserLocation 方法,但没有运气。

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

【问题讨论】:

    标签: ios swift3 mkmapview core-location


    【解决方案1】:

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

    要移动地图 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() 需要一个弧度值,在上面的示例中,我们通过将度数乘以半圆数将度数转换为弧度。

    以下链接真的很有帮助:

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

    【讨论】:

      猜你喜欢
      • 2021-12-04
      • 1970-01-01
      • 1970-01-01
      • 2011-07-06
      • 1970-01-01
      • 2013-03-24
      • 2022-08-19
      • 1970-01-01
      • 2022-10-08
      相关资源
      最近更新 更多