在地图上放置经度和纬度

时间:2018-07-06 17:59:58

标签: ios swift mapkit

我试图通过插入纬度和经度来显示我在mapView中想要的地方,但是我没有做到这一点,地图总是向我显示我所在的位置,而不是我想要的位置

这些是我使用的代码

class MapViewController: UIViewController, CLLocationManagerDelegate{
    @IBOutlet weak var map: MKMapView!
    let manager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        map.showsPointsOfInterest = true
        map.showsUserLocation = true
        manager.requestAlwaysAuthorization()
        manager.requestWhenInUseAuthorization()
        //user location stuff
        if CLLocationManager.locationServicesEnabled() {
            manager.delegate = self
            manager.desiredAccuracy = kCLLocationAccuracyBest
            manager.startUpdatingLocation()
        }
    }

    func locationManager(_manager: CLLocationManager, didUpdateLocations locations:[CLLocation]) {
        let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
        let location = CLLocationCoordinate2D(latitude: 36.1070, longitude: -112.1130)
        let region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)
        map.setRegion(region, animated: true)
        self.map.showsUserLocation = true
    }
}

2 个答案:

答案 0 :(得分:0)

对此进行评论

map.showsUserLocation = true

并将didUpdateLocations的代码放入viewDidLoad

class MapViewController: UIViewController, CLLocationManagerDelegate{
    @IBOutlet weak var map: MKMapView!
    let manager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        map.showsPointsOfInterest = true
        //map.showsUserLocation = true
        manager.requestAlwaysAuthorization()
        manager.requestWhenInUseAuthorization()
        //user location stuff
        if CLLocationManager.locationServicesEnabled() {
            manager.delegate = self
            manager.desiredAccuracy = kCLLocationAccuracyBest
            manager.startUpdatingLocation()
        }

        let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
        let location = CLLocationCoordinate2D(latitude: 36.1070, longitude: -112.1130)
        let region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)
        map.setRegion(region, animated: true)
    }

    func locationManager(_manager: CLLocationManager, didUpdateLocations locations:[CLLocation]) {
         ///
    }
}

答案 1 :(得分:0)

要获得更可靠和有效的结果,

我建议使用.plist(属性列表)文件动态地存储纬度和经度,这样会容易得多,并且花费的时间更少。 plist包含管脚注释方向,它们基本上是一个XML文本文件,其中包含用于捆绑软件执行的基本配置信息。 这是将其附加到您的项目的方法: 在主视图控制器中编写以下函数:

TCP Proxy

enter image description here