I don't understand how to change the image of a mark point in Swift. I have tried a few variations of
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView!
but it seems to never get called and I don't know how to force it to be called.
I will say I added the MKMapView
programmatically too so I can switch sizes (horizontal and vertical) based on which iPhone is being used. (I still can't believe this isn't intuitive to Xcode and the programmer has to program around it.)
Here is my code:
@IBAction func addStroke(sender: UIButton) {
NSLog("Add Stroke Touched")
NSLog("X %f Y %f", manager.location.coordinate.latitude, manager.location.coordinate.longitude)
GPSspot = manager.location.coordinate
annotation.setCoordinate(GPSspot)
annotation.title = "Stroke"
let useID = "test"
var myAnnotation = mapHole.dequeueReusableAnnotationViewWithIdentifier(useID)
if (myAnnotation == nil) {
myAnnotation = MKAnnotationView(annotation: annotation, reuseIdentifier: useID)
myAnnotation.image = UIImage(named: "ballSmall.png")
myAnnotation.canShowCallout = true
myAnnotation.enabled = true
} else {
myAnnotation.annotation = annotation
}
mapHole.viewForAnnotation(annotation)
mapHole.addAnnotation(annotation)
}
//MARK: - Map Kit
var mapRect = MMRect(xLoc: 502, yLoc:20, yWidth:218, xHeight:386)
var mapHole:MKMapView! //= MKMapView() as MKMapView
var annotation = MKPointAnnotation()
var MAView:MKAnnotationView!
//MARK: - GPS Locations
var manager:CLLocationManager!
var GPSspot:CLLocationCoordinate2D!
var span:MKCoordinateSpan!
var region:MKCoordinateRegion!
//MARK: - Default Initializer
override func viewDidLoad() {
super.viewDidLoad()
//MARK: Locations
manager = CLLocationManager()
if (CLLocationManager.locationServicesEnabled()) {
NSLog("locations services started")
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestAlwaysAuthorization()
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
} else {
NSLog("location services failed")
}
//MARK: MKMapView
mapHole = MKMapView(frame: mapRect.textRect)
span = MKCoordinateSpanMake(0.001, 0.001)
GPSspot = manager.location.coordinate
region = MKCoordinateRegion(center: GPSspot, span: span)
mapHole.setRegion(region, animated: true)
mapHole.mapType = MKMapType.Satellite
mapHole.showsUserLocation = true
self.view.addSubview(mapHole)
}
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if annotation is MKUserLocation {
return nil
}
let reuseId = "test"
var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if anView == nil {
anView = MKAnnotationView (annotation: annotation, reuseIdentifier: reuseId)
anView.image = UIImage(named:"ballSmall.png")
anView.canShowCallout = true
} else {
anView.annotation = annotation
}
return anView
}
Can anyone help? Thank you! I have been working on this and reading for about a week now and I just don't get it.