在iOS地图中更改引脚方向 [英] Change Pin direction in iOS Map

查看:97
本文介绍了在iOS地图中更改引脚方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • SWIFT 3.0

  • MKMAPVIEW

  • iOS

  • SWIFT 3.0
  • MKMAPVIEW
  • iOS

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

Note : - (Integrated AppleMap ,Not working with GoogleMap)

我已完成以下操作:


  1. 已实施地图并将自定义图像添加到用户位置注释

  2. 当地图打开时,它会在正确的位置显示用户位置

我的要求:


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

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.

例如:如果Boat显示在User Location位置并指向North,但如果用户移向West,那么boat(User Location Pin)也应该指向那个方向。

E.g : If Boat is showing at User Location position and its pointing toward North but if user move toward West then boat (User Location Pin) also should point to that direction.

尝试用佛llowing代码:

Tried with following Code :

//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 方法,但没有运气。

Called animateUserLocation method from didUpdateLocation Method but no Luck.

请分享您的建议我做错了什么。在此先感谢。

Kindly share your suggestion what i am doing wrong . Thanks in advance.

推荐答案

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

Understanding iOS's location concepts completely helps to resolve any issue regarding AppleMap.

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

To move map Pin or AnnotationView , we can use the CoreLocation framework which outputs data related to the users current location.

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 将输出一个表示设备指向的方向的值。

In the above example, "heading.magneticHeading" will output a value representing the direction the device is pointed at.


  • 0表示北

  • 90表示东

  • 180表示南

  • 270表示西

  • 其他所有内容

  • 0 means north
  • 90 means east
  • 180 means south
  • 270 means west
  • everything else in between

下一步是使用这些值并相应地旋转 AnnotationView Pin

The next step is to use those values and rotate your AnnotationView or Pin accordingly.

CGAffineTransformMakeRotation 可以帮助解决这个问题。

CGAffineTransformMakeRotation can help with this.

例如,如果你想要旋转到imageview指向东北,这需要学位值45,你的代码可能看起来像这样。

For example if you want to rotate to imageview to point to northeast, which would require a degree value of 45, your code might look something like this.

float degrees = 45 
imageView.transform = CGAffineTransformMakeRotation(degrees * M_PI/180)

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

Just be aware that CGAffineTransformMakeRotation() expects a radian value, in the example above we've converted degrees to radians by multiplying degrees with the number of half circles.

以下链接真有帮助:

  • https://stackoverflow.com/a/7634232/3400991

最后解决了我的问题。希望这个完整的答案也能帮助其他人。

Finally Resolved my issue. Hope this complete answer helps other too.

这篇关于在iOS地图中更改引脚方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆