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

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      

Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn 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.