gpt4 book ai didi

ios - 即使使用 set delegate,MKMapview delegate 也从未被调用

转载 作者:行者123 更新时间:2023-11-28 07:25:59 25 4
gpt4 key购买 nike

我有一个集成 ScrollView 的 UIView。在 ScrollView 中,我有一张 map ,用户需要在其中移动图钉。我稍后会重新使用 pin 的位置。

我正在为 map 设置强引用和设置委托(delegate)(包括设计和代码)。我能够看到创建的图钉但具有自定义图标和将其设置为可拖动的可能性,我已经实现了 mapView( viewFor MKAnnotation

这似乎从未被调用过,我不明白为什么。

我还尝试让另一个委托(delegate)作为 didfinishloading 来了解问题是否与 mkannotation 有关,但这个委托(delegate)从未被调用过。

import Foundation
import Firebase
import MapKit
import UIKit


public class Setup: UIViewController,CLLocationManagerDelegate,MKMapViewDelegate{


var ChargePointID = ""
@IBOutlet weak var chargepointIDLabel: UILabel!


let locationManager = CLLocationManager()
var pin: customPin!
//setting up variables
//pricing textfield and slider
@IBOutlet weak var priceLabel: UITextField!
@IBOutlet weak var priceSlider: UISlider!


@IBOutlet var map: MKMapView!

override public func viewDidLoad() {
super.viewDidLoad()
chargepointIDLabel.text = ChargePointID
self.map.delegate = self
self.map.mapType = .hybrid
// Ask for Authorisation from the User.
self.locationManager.requestAlwaysAuthorization()

// For use in foreground
self.locationManager.requestWhenInUseAuthorization()

if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}

//create liocation relative to user
let location = CLLocationCoordinate2D(latitude: (locationManager.location?.coordinate.latitude)!, longitude: (locationManager.location?.coordinate.longitude)!)

//centering map to user location
let region = MKCoordinateRegion(center: location, span: MKCoordinateSpan(latitudeDelta: 0.005, longitudeDelta: 0.005))
self.map.setRegion(region, animated: true)

//creating a pin of type custompin
pin = customPin(pinTitle: ChargePointID, pinSubTitle: "", location: location)

map.addAnnotation(pin)


}

private func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
print("locations = \(locValue.latitude) \(locValue.longitude)")
}



private func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
print("annotation title == \(String(describing: view.annotation?.title!))")
}


private func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

print("ok")
let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "customannotation")
annotationView.image = UIImage(named:"setuppin")
annotationView.canShowCallout = true
return annotationView

}

func mapViewDidFinishLoadingMap(_ mapView: MKMapView) {
print(" map has finished")
}


private func map(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

print("ok")
let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "customannotation")
annotationView.image = UIImage(named:"setuppin")
annotationView.canShowCallout = true
return annotationView

}







}

class customPin: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var title: String?
var subtitle: String?

init(pinTitle:String, pinSubTitle:String, location:CLLocationCoordinate2D) {
self.title = pinTitle
self.subtitle = pinSubTitle
self.coordinate = location
}
}

最佳答案

private 不是必需的。替换

private func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

关于ios - 即使使用 set delegate,MKMapview delegate 也从未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56590261/

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