Change the direction of output on the iOS card

  • SWIFT 3.0
  • MKMAPVIEW
  • IOS

Note: - (integrated AppleMap, does not work with GoogleMap)

I have done the following:

  • Implemented map and added custom image to annotate user location
  • When the map is open, it shows the user's location in the right place.

My requirements:

When the user moves in another direction, being in the same place (or in another place), the contact (in the current location) should automatically indicate the direction in which the user is pointing.

For example: If the boat is displayed at the user's location and points to the north, but if the user is moving toward the west, then the boat (contact of the user's location) should also indicate this direction.

Tried the 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()
    }

Called animateUserLocationby a method method didUpdateLocation, but without luck.

Please share your suggestion on what I am doing wrong. Thanks in advance.

0
source share
1 answer

Understanding the concepts of location iOS'scompletely helps to solve any problem related to 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

- AnnotationView Pin.

CGAffineTransformMakeRotation .

, , -, 45, .

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

, CGAffineTransformMakeRotation() . , .

:

, . , .

+4

Source: https://habr.com/ru/post/1664294/


All Articles