在swift iOS中获取准确的位置

时间:2015-12-02 12:52:02

标签: ios swift core-location

我为当前位置编写了Swift代码,但代码并未提供Android用于定位的确切位置。

对我来说,它只是给我指向国家位置,但我需要我申请的确切位置。

这是我的位置代码

import UIKit
import MapKit
import CoreLocation
class location: UIViewController , CLLocationManagerDelegate {
    @IBOutlet weak var map: MKMapView!
    var locationManager: CLLocationManager!
    var address : String = "" 
    override func viewDidLoad() {
        super.viewDidLoad()  
        //let dataProvider = GoogleDataProvider(            
        if (CLLocationManager.locationServicesEnabled())
        {
            locationManager = CLLocationManager()
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.requestWhenInUseAuthorization()
            locationManager.startUpdatingLocation()
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    var start : CLLocation!
    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        start = locations.last
        let center = CLLocationCoordinate2D(latitude: start!.coordinate.latitude, longitude: start!.coordinate.longitude)
        let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
        self.map.setRegion(region, animated: true)      
        print("function called")

        let annotation = MKPointAnnotation()

        annotation.coordinate = center
        annotation.title  = "You are Here :)" 
        map.addAnnotation(annotation)
        let geocoder = CLGeocoder()
        geocoder.reverseGeocodeLocation(manager.location!, completionHandler: {
            placemarks, error in
            if (error != nil)
            {
                print("Reverse geocoder failed with error" + error!.localizedDescription)
                return
            }

            if let pm = placemarks?.first {
                self.displayLocationInfo(pm)
            }
            else
            {
                print("Problem with the data received from geocoder")
            }
        })
    }
}

0 个答案:

没有答案