dimanche 26 avril 2015

How to rotate a Direction Arrow to particular location for (swift)


my app rotate an image to particular location but it's not work and the arrow rotate to the north , i want the arrow rotate to the particular location

please i want swift not objective-c and i check to another questions but all questions here for objective-c and i try to make the same this questions on the link enter link description here it's doesn't work

class ViewController: UIViewController,CLLocationManagerDelegate{

var direction = []
var coordinate = CLLocationCoordinate2D()
var radiansValue = CGFloat()
var angle = CGFloat()
var locationManager = CLLocationManager()
var arrowImageView : UIImageView? = UIImageView()
var latitudeOfTargetedPoint = CLLocationDegrees()
var longitudeOfTargetedPoint = CLLocationDegrees()

func DegreesToRadians (value: Double) -> CGFloat {
    return CGFloat(value * M_PI / 180.0)
}


func RadiansToDegrees (value: Double) -> CGFloat {
    return CGFloat(value * 180.0 / M_PI)

}

override func viewDidLoad() {
    super.viewDidLoad()

    arrowImageView!.frame = CGRect(x: 100, y: 200, width: 100, height: 100)
    arrowImageView!.image = UIImage(named: "arrow.png")
    view.addSubview(arrowImageView!)

    if CLLocationManager.locationServicesEnabled() {

        println("CLLocationManager.locationServicesEnabled")

        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        self.locationManager.distanceFilter = 100.0;

        self.locationManager.startUpdatingLocation()
        self.locationManager.startUpdatingHeading()

        latitudeOfTargetedPoint = 48.021199
        longitudeOfTargetedPoint = 29.264888

        var coordinate = CLLocationCoordinate2DMake(latitudeOfTargetedPoint, longitudeOfTargetedPoint)

        var latDelta:CLLocationDegrees = 0.01
        var lonDelta:CLLocationDegrees = 0.01

        var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
        var region:MKCoordinateRegion = MKCoordinateRegionMake(coordinate, span)


        println(calculateAngle)
    }

}//29.264888, 48.021199

// Caculate the angle between the north and the direction to observed geo-location

func calculateAngle (userlocation: CLLocation) -> CGFloat{

    var userLocationLatitude = DegreesToRadians(userlocation.coordinate.latitude)
    var userLocationLongitude = DegreesToRadians(userlocation.coordinate.longitude)

    var targetedPointLatitude = DegreesToRadians(latitudeOfTargetedPoint);
    var targetedPointLongitude = DegreesToRadians(longitudeOfTargetedPoint)

    var longitudeDifference = targetedPointLongitude - userLocationLongitude
    var y = sin(longitudeDifference) * cos(targetedPointLatitude)
    var x = cos(userLocationLatitude) * sin(targetedPointLatitude) - sin(userLocationLatitude) * cos(targetedPointLatitude) * cos(longitudeDifference);

    var radiansValue = atan2(y, x)

    if radiansValue < 0.0 {
        var temFloast = Float(radiansValue)

        temFloast = temFloast + Float(2 * M_PI);
        radiansValue = CGFloat(temFloast)

        //radiansValue += 2*M_PI;

                }

    return radiansValue

}

func locationManager(manager: CLLocationManager!, didUpdateToLocation newLocation: CLLocation!, fromLocation oldLocation: CLLocation!) {

    self.angle = calculateAngle(oldLocation!)

     }


func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
    println(error.localizedDescription)
}

func locationManager(manager: CLLocationManager!, didUpdateHeading newHeading: CLHeading!) {


    var  direction = newHeading.magneticHeading
    angle = RadiansToDegrees



    if (direction > 180){
        direction = 360 - direction;
    }else{
        direction = 0 - direction;
    }

    //    Rotate the arrow image
    if arrowImageView != nil {

        UIView.animateWithDuration(1.0, animations: { () -> Void in
            self.arrowImageView!.transform = CGAffineTransformMakeRotation(self.DegreesToRadians(direction) + self.angle)
        })

    }

}


}


Aucun commentaire:

Enregistrer un commentaire