【问题标题】:Current Location Map Pin When Button is Pressed按下按钮时的当前位置图引脚
【发布时间】:2017-01-14 20:47:13
【问题描述】:

我一直在尝试制作一个应用程序,当按下标记结果按钮时,会在用户的当前位置弹出一个地图图钉。

问题是,每当我在 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

【问题讨论】:

  • 你能添加你想要的截图吗,为什么你把 didupdatelocation 放在这个动作方法下,它是一个被称为 evrytime 的委托

标签: swift xcode


【解决方案1】:

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

self.mapView.addAnnotation(annotationz)行之后,添加这行:manager.stopUpdatingLocation(),否则它会继续更新你的位置,从而调用委托方法,并添加另一个pin。

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

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()

【讨论】:

  • 非常感谢。这真的很有帮助
猜你喜欢
  • 2017-06-29
  • 2016-03-27
  • 1970-01-01
  • 2017-01-15
  • 1970-01-01
  • 2015-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多