TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

MapView User Location

Forums > SwiftUI

Hello

If I create a simple MapView and associated delegate and have the following my user location is shown as the standard blue dot.

func makeUIView(context: Context) -> MKMapView {

        let region: MKCoordinateRegion = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: -37.8136, longitude: 144.9631), span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

        mapView.delegate = context.coordinator

        mapView.region = region

        mapView.showsUserLocation = true
        mapView.showsScale = true

        return mapView
    }

I then wanted to add some annotations so along iwth other stuff I add the following to the delegate

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
            let annotationView = MarkerAnnotationView(annotation: annotation, reuseIdentifier: "Marker")
            annotationView.canShowCallout = false
            return annotationView
        }

When I do this the user location disappears.

Anyone know why.

Regards

3      

Yesterday, I was watching a great video by Stewart Lynch about new button styles in iOS 15.

Here's a link: iOS 15 Button Styles

How does this relate to your question about Maps?

At minute 17:05 Stewart shows some code for getting permissions to access a user's current position. This is because he is demontrating a new button type, the LocationButton. Futhermore, he has some nice code snips that I think directly relate to your question. At minute 21, he shows a map manager class. You may find this class very useful for what you're doing!

In short, he demonstrates showing a user's location, and also tracking the user's location.

Report back! We all want to hear what you think of his LocationManager class.

3      

Sean Allen did a course where he customized the pins in Dub Dub Grub.

3      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.