UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: centre coordinates from visible map?

Forums > SwiftUI

Hi, I'm struggling with SwiftUI and the new MapKit (ios 17).

    @State private var cameraPosition: MapCameraPosition = .automatic

I create a camera position

  Map(position: $cameraPosition) 

I create the map and everything is working as it should

Later the user has moved around the map and I want to catch the coordinates from the centre of the map. I do it from a func in the same struc.

      let centre = cameraPosition.region?.center

I've tried all different variants that cameraPosition offers, but I always reseive NIL

How can I catch the coordinates from the centre of the map?

3      

I use the same code to retrieve the centre coordinate with no problems. Are you absolutely sure you aren't creating a new, local, undefined, cameraPosition somewhere? Also, is the region or the coordinate equal to nil?

3      

Yes I'm sure, no local cameraPosition defined and undefined. I get nil retured on both cameraPosition.region?.center and cameraPosition.region. Must have done something stupid elsewhere(?)

cheers

3      

My use case involves updating the map to track the user's location with CoreLocation and that updates the cameraPosition.region?.center just fine. I just checked my project code and if I halt the user tracking and just pan the map, the centre location returns nil so my earlier answer seems a bit premature.

3      

thanks for checking and letting me know

3      

This seems to work.

import MapKit
import SwiftUI

struct ContentView: View {

    @State private var cameraPosition: MapCameraPosition = .region(MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 40, longitude: 40), span: MKCoordinateSpan(latitudeDelta: 0.25, longitudeDelta: 0.25)))

    var body: some View {
        Map(position: $cameraPosition)
            .onMapCameraChange { mapCameraUpdateContext in
                print("\(mapCameraUpdateContext.camera.centerCoordinate)")
            }
    }
}

3      

great stuff, works as a charm. Cheers :)

3      

Thanks @magnas35

What if I want to read the region - or more specifically the span of the map in meters?

3      

@tonyayoub

I'm afraid I don't have a great answer for you, but the mapCameraUpdateContext also has an MKCoordinateRegion property (.region) from which you can access the .span property (MKCoordinateSpan). That gives you the .latitudeDelta and .longitudeDelta properties. These give you the span dimensions in degrees that you could use to calculate the dimensions in metres.

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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

Reply to this topic…

You need to create an account or log in to reply.

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.