Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在swift iOS中获取准确位置_Ios_Swift_Core Location - Fatal编程技术网

在swift iOS中获取准确位置

在swift iOS中获取准确位置,ios,swift,core-location,Ios,Swift,Core Location,我已经为当前位置编写了Swift代码,但该代码没有给出Android用于定位的确切位置 对我来说,它只是给我的国家位置指向,但我需要我的应用程序的确切位置 这是我的位置代码 import UIKit import MapKit import CoreLocation class location: UIViewController , CLLocationManagerDelegate { @IBOutlet weak var map: MKMapView! var locati

我已经为当前位置编写了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")
            }
        })
    }
}

您正在模拟器或设备上进行测试?如果您在模拟器上测试,它将不会显示确切的位置。使用实际设备。但是,当我给出.gpx文件时,它根据给定的坐标来定位……你需要考虑iOS!安卓MapKit无法像google maps iOS SDK那样工作。非常感谢您提供的信息…:)你是说你看到的文字信息只显示国家?地图上显示的注释呢?显示位置地址的代码在哪里?在您发布的代码中,看起来您正在调用一个方法
displayLocationInfo()
。如果您需要帮助改进代码,请发布该代码。