-1

I'm trying to make a simple by feet navigator, using a arrow image, rotating to indicate the direction to follow with a constant refresh.

Using magneticHeading i found error unexpectedly found nil while unwrapping an Optional value, maybe i forgot something.

How i can use correctly magneticHeading?

var northLatitude :CLLocationDegrees 
var latitude :CLLocationDegrees = 46.0
var longitude :CLLocationDegrees = 7.0


func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {

    longitude = manager.location.coordinate.longitude
    latitude = manager.location.coordinate.latitude

    var buildingLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
    var regionOfInterest:MKCoordinateRegion = MKCoordinateRegionMake(buildingLocation, span)

    self.mapView.setRegion(regionOfInterest, animated: true)
    self.mapView.addAnnotation(buildingAnnotation)

    northLatitude = manager.heading.magneticHeading  //ERROR
}
Antonio Viscomi
  • 227
  • 2
  • 5
  • 15

1 Answers1

0

You can calculate the angle at which the arrow should point and then use the transform property of the image view to change direction. Pseudo code will be something like this.

func onPositionChange()
{
    UIView.animateWithDuration(1.0, animations:
        {
            var angle_in_degrees:CGFloat = 10 // Calculated Angle.
            var angle_in_radians = (angle_in_degrees * 3.1415) / 180.0
            self.imgView.transform = CGAffineTransformMakeRotation(angle_in_radians)
    })
}
rakeshbs
  • 24,392
  • 7
  • 73
  • 63