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?


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?


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(?)



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.


thanks for checking and letting me know


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


great stuff, works as a charm. Cheers :)


Thanks @magnas35

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



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.


Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.