自定义地理位置的简单罗盘方向

时间:2015-04-07 23:08:34

标签: ios iphone swift geolocation compass-geolocation

一个简单的指南针,指向您可以通过提供位置的纬度和经度指定的自定义地理位置的方向。 我尝试但有错误 我不知道这个错误在哪里

class ViewController:UIViewController,CLLocationManagerDelegate { var locationmanager:CLLocationManager!

@IBOutlet var arrowImageView: UIImageView!

var currentHeading = CLHeading()

var userLocation: CLLocationCoordinate2D!
var destLocation: CLLocationCoordinate2D!

var latitudeOfTargetedPoint = 48.858093
var longitudeOfTargetedPoint = 2.294694

var angle:Float!

override func viewDidLoad() {
    super.viewDidLoad()

   locationmanager = CLLocationManager()
    locationmanager.delegate = self
    locationmanager.desiredAccuracy = kCLLocationAccuracyBest
    locationmanager.requestWhenInUseAuthorization()
    locationmanager.startUpdatingLocation()
    locationmanager.distanceFilter = 100.0
    locationmanager.startUpdatingHeading()
}

func showUserAndDestinationOnMap()
{
    // Calculate the rectangle that contains the current position and the destination
    var maxLat:Double = fmax(userLocation.latitude,  latitudeOfTargetedPoint)
    var maxLon:Double = fmax(userLocation.longitude, longitudeOfTargetedPoint)

    var minLat:Double = fmin(userLocation.latitude,  latitudeOfTargetedPoint)
    var minLon:Double = fmin(userLocation.longitude, longitudeOfTargetedPoint)

    // Calculate the latitude, longitude width when displaying map
    var mapMargin:Double = 1.5;  // Width route enters (1.0) + margin (0.5)
    var leastCoordSpan:Double = 0.005;    // Enlarge maximum value when the display
    var span_x:Double = fmax(leastCoordSpan, fabs(maxLat - minLat) * mapMargin);
    var span_y:Double = fmax(leastCoordSpan, fabs(maxLon - minLon) * mapMargin);

    var span:MKCoordinateSpan = MKCoordinateSpanMake(span_x, span_y);

    // Calculate the center of the destination your location
    var center:CLLocationCoordinate2D = CLLocationCoordinate2DMake((maxLat + minLat) / 2, (maxLon + minLon) / 2);
    var region:MKCoordinateRegion = MKCoordinateRegionMake(center, span);




       }


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

    self.showUserAndDestinationOnMap()


}


func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
    println(error)
}
func locationManager(manager: CLLocationManager!, didUpdateHeading newHeading: CLHeading!) {

    var degrees = newHeading.magneticHeading

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

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

        UIView.animateWithDuration(3.0, animations: { self.arrowImageView.transform = CGAffineTransformRotate(self.arrowImageView.transform,self.DegreesToRadians(degrees) )  })

    }

}

func DegreesToRadians(价值:双倍) - > CGFloat {         返回CGFloat(值* M_PI / 180.0)     }

func RadiansToDegrees(value:Double) - > CGFloat {         返回CGFloat(值* 180.0 / M_PI)}

如果有人可以这样做

抱歉我的英文:P~

0 个答案:

没有答案