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

New MapKit API in iOS 17

Forums > SwiftUI

@CR7  

I'm having a deprecation problem on the Map and MapAnnotation. Can someone please help me update this code? I've searched numerous sources, but none have helped.

struct MapPreview: View {
    @State private var region: MKCoordinateRegion = {
        var mapCoordinates = CLLocationCoordinate2D(
            latitude: 6.600286,
            longitude: 16.4377599
        )
        var mapZoomLevel = MKCoordinateSpan(
            latitudeDelta: 70.0,
            longitudeDelta: 70.0
        )
        var mapRegion = MKCoordinateRegion(
            center: mapCoordinates,
            span: mapZoomLevel
        )

        return mapRegion
    }()

    let locations: [NationalParkLocation] = Bundle.main.decode("locations.json")

    var body: some View {
        Map(coordinateRegion: $region, annotationItems: locations) { location in
            MapAnnotation(coordinate: location.location) {
                MapAnnotationView(location: location)
            }
        }
        .overlay(alignment: .top) {
            HStack(alignment: .center, spacing: 12) {
                Image("compass")
                    .resizable()
                    .scaledToFit()
                    .frame(width: 48, height: 48, alignment: .center)

                VStack(alignment: .leading, spacing: 3) {
                    HStack {
                        Text("Latitude:")
                            .font(.footnote)
                            .bold()
                            .foregroundStyle(.tint)

                        Spacer()

                        Text("\(region.center.latitude)")
                            .font(.footnote)
                            .foregroundStyle(.white)
                    }

                    Divider()

                    HStack {
                        Text("Longitude:")
                            .font(.footnote)
                            .bold()
                            .foregroundStyle(.tint)

                        Spacer()

                        Text("\(region.center.longitude)")
                            .font(.footnote)
                            .foregroundStyle(.white)
                    }
                }
            }
            .padding(.vertical, 12)
            .padding(.horizontal, 16)
            .background(
                Color.black
                    .cornerRadius(8)
                    .opacity(0.6)
            )
            .padding()
    }
}

3      

First, you might consider adding comments to your code and highlight the section where you're finding deprecation issues.

Next, in your code, write a few comments about what you're trying to accomplish.

Next, provide the error codes you're receiving. Do you understand the deprecation messages? Maybe the compiler is telling you EXACTLY what you need to do, but you're still learning SwiftUI and maybe you don't understand the compiler's message.

We can help you. But provide us some hints, if you please!

3      

In general, if you're encountering deprecation warnings related to Map and MapAnnotation, here are some potential steps to consider: Check Documentation: Look at the official documentation or release notes for the framework or library you're using. Often, these sources will indicate which methods or classes are deprecated and suggest alternatives. www.quickprinting.ae Update to Latest Version: Ensure that you're using the latest version of the framework or library. Deprecated methods are usually replaced by newer, updated ones in subsequent versions. Use Replacement Methods: When methods or classes are deprecated, there's typically a replacement recommended by the developers. Update your code to use the suggested replacement methods or classes. Search Forums or Communities: Browse developer forums, discussion boards, or communities related to the framework or language you're using. Other developers might have faced similar issues and could offer solutions or workarounds.

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.