gpt4 book ai didi

swift - 按下按钮时的当前位置 map 图钉

转载 作者:行者123 更新时间:2023-11-28 12:32:51 27 4
gpt4 key购买 nike

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

问题是,每当我在 IBAction 中调用 DidUpdateLocations 时, map 注释永远不会出现。但是,如果这个函数在 IBAction 之外,它就可以完美地工作。当我在 IBAction 外部和内部调用该函数时,会显示连续的 map 图钉流。

这是我的代码:

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

}


}

这是我的主要 Storyboard:

Main.Storyboard

最佳答案

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

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

不是在 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() 另行通知它为止。

关于swift - 按下按钮时的当前位置 map 图钉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41654846/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com