按下按钮时的当前位置图引脚

时间:2017-01-14 20:47:14

标签: swift xcode

我一直试图创建一个应用,当按下标记发现按钮时,会在用户的当前位置弹出一个地图引脚。

问题在于,每当我在IBAction中调用DidUpdateLocations时,地图注释都不会显示出来。但是,如果它在IBAction之外,则此功能可以正常工作。当我在IBAction的外部和内部调用该函数时,会显示连续的地图引脚流。

这是我的代码:

import UIKit
import MapKit
import CoreLocation


class SecondViewController: UIViewController, CLLocationManagerDelegate,MKMapViewDelegate {
@IBOutlet var mapView: MKMapView!
let manager = CLLocationManager()
override func viewDidLoad() {
    super.viewDidLoad()
    manager.delegate = self
    manager.desiredAccuracy = kCLLocationAccuracyBest
    manager.requestWhenInUseAuthorization()
    manager.startUpdatingLocation()

}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

@IBAction func markStuff(_ sender: Any) {
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let location = locations[0]
        let span:MKCoordinateSpan = MKCoordinateSpanMake(0.001, 0.001)
        let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
        let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
        let annotation = MKPointAnnotation()
        annotation.coordinate = location.coordinate
        mapView.setRegion(region, animated: true)
        self.mapView.addAnnotation(annotationz)
    }

}


}

这是我的main.storyboard:

Main.Storyboard

1 个答案:

答案 0 :(得分:2)

首先,将委托函数locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])移到@IBAction func markStuff(_ sender: Any)

之外

在行self.mapView.addAnnotation(annotationz)之后,添加以下行:manager.stopUpdatingLocation(),否则它将继续更新您的位置,从而调用委托方法,并添加其他图钉。

不要在manager.startUpdatingLocation()中调用viewDidLoad,而是在按下按钮时调用它。更新的代码应如下所示:

import UIKit
import MapKit
import CoreLocation


class SecondViewController: UIViewController, CLLocationManagerDelegate,MKMapViewDelegate {
    @IBOutlet var mapView: MKMapView!
    let manager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()
        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.requestWhenInUseAuthorization()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    @IBAction func markStuff(_ sender: Any) {

        manager.startUpdatingLocation()

    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let location = locations[0]
        let span:MKCoordinateSpan = MKCoordinateSpanMake(0.001, 0.001)
        let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
        let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
        let annotation = MKPointAnnotation()
        annotation.coordinate = location.coordinate
        mapView.setRegion(region, animated: true)
        self.mapView.addAnnotation(annotationz)
        manager.stopUpdatingLocation()
    }

}

locationManager(manager:didUpdate...)未被调用,因为它位于按钮的操作方法中...因为locationManager已更新位置,所以正在调用它。它将继续更新您的位置,直到您另外通过调用manager.stopUpdatingLocation()来说明。