Home > other >  Swiftui mapmarker missing arguments error
Swiftui mapmarker missing arguments error

Time:04-06

I'm making an application in swiftui, I added maps, but I'm having trouble with mapmarker, can you help?

The error is at the bottom of the code section.

//
//  depremHaritasi.swift
//  deprembilgisistemi
//
//  Created by Tugay Asan on 1.03.2022.
//

import SwiftUI
import MapKit


struct depremHaritasi: View {

    
    let id: UUID
        let location: CLLocationCoordinate2D
        init(id: UUID = UUID(), lat: Double, long: Double) {
            self.id = id
            self.location = CLLocationCoordinate2D(
                latitude: lat,
                longitude: long)
        }
    
        @State var quakes: [EarthQuake] = []
    
        @State var coordinateRegion = MKCoordinateRegion(
              center: CLLocationCoordinate2D(latitude: 38.9520281, longitude: 35.6980142),
              span: MKCoordinateSpan(latitudeDelta: 30, longitudeDelta: 10))

            var body: some View {
                Map(coordinateRegion: $coordinateRegion)
                .edgesIgnoringSafeArea(.all)
                .onAppear {
                Api().getEarthQuake { (quakes) in
                    self.quakes = quakes
                }
            }
    }
}


struct depremHaritasi_Previews: PreviewProvider {
    static var previews: some View {
        depremHaritas()   //ERROR//Missing arguments for parameters 'lat', 'long' in call
    }
}

I tried adding the mapmarker but got a "missing arguments" error. I tried looking at different forums but it didn't work. I want the mapmarker to appear on the map.

CodePudding user response:

struct depremHaritas init takes 3 arguments id ,lat and long. Argument id has a default value so it can be skipped , but however, lat and long don't have default values. You have to explicitly provide the value to when you initialize.

init(id: UUID = UUID(), lat: Double, long: Double)

So , your code should be :

struct depremHaritasi_Previews: PreviewProvider {
    static var previews: some View {
        depremHaritas(lat:0.0 , long:0.0)
    }
}

Or another way is to provide default values for lat and long arguments in init as below :

init(id: UUID = UUID(), lat: Double = 0.0, long: Double = 0.0)

In this case, your will not face error for your call.

  • Related