Solved Create custom Apple Maps user location button

52 views Asked by At

I was working on this I didn't find any documentation and so I thought I would create some. After I solved it I was like oh that was easier than I thought.

Anyway, I wanted to create the 3 button states I saw on apple maps. I tried a few setRegion things. After some work realized how apple intended its use. So I thought I would document it here.

1

There are 1 answers

0
jeremy wilson On
    func locationButtonWasPressed() {        
        switch self.coreMapView.userTrackingMode {
        case .none:
            self.coreMapView.userTrackingMode = .follow
        case .follow:
            self.coreMapView.userTrackingMode = .followWithHeading
        case .followWithHeading:
            self.coreMapView.userTrackingMode = .none
        @unknown default:
            print("userTrackingMode undefined")
        }
    }
    func mapView(_ mapView: MKMapView, didChange mode: MKUserTrackingMode, animated: Bool) {        
        switch mode {
        case .none:
            let tintedImage = #imageLiteral(resourceName: "nearby").withRenderingMode(.alwaysTemplate)
            locationButton.image = tintedImage
        case .follow:
            let tintedImage = #imageLiteral(resourceName: "nearby-selected").withRenderingMode(.alwaysTemplate)
            locationButton.image = tintedImage
        case .followWithHeading:
            let tintedImage = #imageLiteral(resourceName: "scout").withRenderingMode(.alwaysTemplate)
            locationButton.image = tintedImage
        @unknown default:
            print("userTrackingMode undefined")
        }
    }