Using extension to store user location in a useable variable

43 views Asked by At

I have an extension that I created using LocationManager to store the users location in a variable to be used in Swiftui Map() and my extension. I noticed that when I init thee variable both lat and long are required by the extension to be used where I just need userLat to be needed.

latitude: userLocation.init(userLat: <#Double#>, userLong: <#Double#>).self.userLat,

   struct ContentView_v2: View {
    
    //    MARK: Get current location
        class userLocation {
            let locationManager = LocationManager()
    
            var userLat: Double
            var userLong: Double
    
            init(userLat: Double, userLong: Double){
                self.userLat = locationManager.lastLocation?.coordinate.latitude ?? 0.0
                self.userLong = locationManager.lastLocation?.coordinate.longitude ?? 0.0
            }
    
        }
    
        @State private var region = MKCoordinateRegion(
            center: CLLocationCoordinate2D(
                latitude: userLocation.init(userLat: <#Double#>, userLong: <#Double#>).self.userLat,
                longitude: userLocation.init(userLat: <#Double#>, userLong: <#Double#>).self.userLong
            ),
            span: MKCoordinateSpan(
                latitudeDelta: 0.01,
                longitudeDelta: 0.01
                )
        )
var body: some View {
        Map(
            coordinateRegion: $region,
            interactionModes: MapInteractionModes.all,
            showsUserLocation: true,
            userTrackingMode: $userTrackingMode,
            annotationItems: yelpbusinessmapdata?.businesses ?? []
        )
        { business in
            MapAnnotation(
                coordinate:
                    business.coordinates.coordinate2D,
                anchorPoint: CGPoint(x: 0.5, y: 0.5)
            ){
                VStack {
                    Image(systemName: "mappin")
                        .font(.system(size: 18, weight: .medium))
                        .foregroundColor(.red)
                    Text(business.name)
                }
                

            }
        }
1

There are 1 answers

0
Asperi On BEST ANSWER

If I correctly understood your code, then probably you wanted the following...

    class userLocation {
        let locationManager = LocationManager()

        var userLat: Double
        var userLong: Double

        init(){
            self.userLat = locationManager.lastLocation?.coordinate.latitude ?? 0.0
            self.userLong = locationManager.lastLocation?.coordinate.longitude ?? 0.0
        }

    }

    @State private var region = MKCoordinateRegion(
        center: CLLocationCoordinate2D(
            latitude: userLocation().userLat,
            longitude: userLocation().userLong
        ),