I'm trying to add a button to then do to a new viewcontroller to my MKPointAnnotations
I've looked closely at these two questions
How to add button to MKPointAnnotation in Swift
How to add a button to the MKPointAnnotation?
and they both have
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation)
-> MKAnnotationView? {
as part of the solution but when I implement this function it is not being called (I'm assuming here that its meant to be called automatically like with cells in tableviews or collectionviews in swift). An insight was to what I should try next would be amazing because overall I feel like this should be easy I'm missing something fundamental.
import UIKit
import Foundation
import MapKit
import CoreLocation
class MapViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
var screenWidth : CGFloat = 0.0
var screenHeight : CGFloat = 0.0
var mapView = MKMapView()
var annotations : [MKPointAnnotation] = []
var annotationView = MKAnnotationView()
let lm = CLLocationManager()
let regionRadius: CLLocationDistance = 1000
var flag : Bool = false
override func viewDidLoad() {
super.viewDidLoad()
screenWidth = UIScreen.main.bounds.width
screenHeight = UIScreen.main.bounds.height
mapView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight)
self.view.addSubview(mapView)
lm.delegate = self
lm.desiredAccuracy = kCLLocationAccuracyBest
lm.requestWhenInUseAuthorization()
lm.startUpdatingLocation()
let detailButton: UIButton = UIButton(type: UIButtonType.detailDisclosure) as UIButton
annotationView.rightCalloutAccessoryView = detailButton
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let coordinateRegion = MKCoordinateRegionMakeWithDistance(locations[locations.count-1].coordinate, regionRadius * 0.5, regionRadius * 0.5)
mapView.setRegion(coordinateRegion, animated: true)
if flag == false {
addAnnotation(latitude: locations[locations.count-1].coordinate.latitude, longitude: locations[locations.count-1].coordinate.longitude)
flag = true
}
}
func addAnnotation(latitude: Double, longitude: Double){
let annotation = MKPointAnnotation()
annotation.coordinate.latitude = latitude
annotation.coordinate.longitude = longitude
annotation.title = "here"
annotations.append(annotation)
mapView.addAnnotation(annotations[0])
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
print("fuddddge not working")
let pinView = MKPinAnnotationView()
return pinView
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}