简体   繁体   中英

How to show current location icon in Swift 3?

I am creating an app in Xcode. I have a different pins on the mapView, but I ant to change one of the pins, to be the current location (the "radar"-looking) icon with a different color showing. How can I do this?

Here is the code I have currently:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {    
    if annotation is MKUserLocation {
        let pin = mapView.view(for: annotation) as? MKPinAnnotationView ?? MKPinAnnotationView(annotation: annotation, reuseIdentifier: nil)
        pin.pinTintColor = #colorLiteral(red: 0.8823529412, green: 0.1647058824, blue: 0.1333333333, alpha: 1)
        return pin
    } else {
        let annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin")  
        annotationView.pinTintColor = #colorLiteral(red: 0.007843137255, green: 0.3803921569, blue: 0.8156862745, alpha: 1)          
        return annotationView
    }
}

Instead of this:

if annotation is MKUserLocation {
    let pin = mapView.view(for: annotation) as? MKPinAnnotationView ?? MKPinAnnotationView(annotation: annotation, reuseIdentifier: nil)
    pin.pinTintColor = #colorLiteral(red: 0.8823529412, green: 0.1647058824, blue: 0.1333333333, alpha: 1)
    return pin
    // ...

just return nil and MapKit will automatically show the user location icon:

if annotation is MKUserLocation {
    return nil
} else {
    // ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM