У меня есть UIView, который объединяет скроллвью. Внутри прокрутки у меня есть карта, где пользователю нужно переместить булавку. Я собираюсь использовать местоположение булавки позже.
Я устанавливаю строгую ссылку и устанавливаю делегата (как дизайн, так и код) для карты. Я могу видеть созданный штифт, но чтобы иметь собственный значок и возможность его перетаскивания, я реализовал mapView (viewFor MKAnnotation
Это, кажется, никогда не называют, и я не понимаю, почему.
Я также пытаюсь получить другого делегата в качестве didfinishload для того, чтобы понять, связана ли проблема с mkannotation, но также этот никогда не вызывался.
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
}
}