How to change annotation image for coordinate?

I do not understand how to change the label image in Swift. I tried several options

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! 

but he seems to never be called, and I do not know how to make him be called.

I will say that I added the program code MKMapView, so I can switch sizes (horizontal and vertical) based on which iPhone is used. (I still cannot believe that this is not intuitive for Xcode, and the programmer needs 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! I have been working on this and reading for about a week, and I just don't get it.

+4
source share
1

viewForAnnotation , delegate , viewDidLoad.

delegate:

mapHole = MKMapView(frame: mapRect.textRect)
mapHole.delegate = self                       // <-- add this line
span = MKCoordinateSpanMake(0.001, 0.001)

, , , MKMapViewDelegate. :

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

, delegate , , .


addStroke, , :

annotation.title = "Stroke"

/* This code doesn't belong here...
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)


viewDidLoad:

GPSspot = manager.location.coordinate
region = MKCoordinateRegion(center: GPSspot, span: span)
mapHole.setRegion(region, animated: true)

, manager.location nil , startUpdatingLocation. , manager.location nil, didUpdateLocations.


, :

... MKMapView , ( ), iPhone. ( , Xcode .)

, Xcode - IDE, . iOS SDK, , iPhone . SDK . "" / " " , WWDC, SO .

+2

All Articles