Home > Blockchain >  How to get the Longitude and Latitude then store it on a Variable? on Swift
How to get the Longitude and Latitude then store it on a Variable? on Swift

Time:07-06

so I've tried to make a program to retrieve the user's location, this time I want to take the Latitude and Longitude that are in the region but how do I retrieve them and store them in a variable

import SwiftUI
import MapKit
import CoreLocationUI

struct ContentView: View {
  @StateObject private var viewModel = ContentViewModel()
    @State var LongStorage : Int = 0
    @State var LangStorage : Int = 0
    var body: some View {
        ZStack(alignment: .bottom){
            Map(coordinateRegion: $viewModel.region, showsUserLocation: true)
                .ignoresSafeArea()
                .tint(Color("GreenColor"))
            
            
            LocationButton(.currentLocation){
                viewModel.requestAllowOnceLocationPermission()
            }
            .foregroundColor(.white )
            .cornerRadius(10)
            .labelStyle(.titleAndIcon)
            .symbolVariant(.fill)
            .tint(Color("GreenColor"))
            .padding(.bottom, 50)
            
            
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

final class ContentViewModel: NSObject, ObservableObject, CLLocationManagerDelegate{
    
    @Published var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude:  40, longitude: 120), span: MKCoordinateSpan(latitudeDelta: 100, longitudeDelta: 100))
    
    let locationManager = CLLocationManager()
    
    override init() {
        super.init()
        locationManager.delegate = self
    }
    
    func requestAllowOnceLocationPermission()  {
        locationManager.requestLocation()
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let latestLocation = locations.first else{
            //Show an error
            return
        }
        DispatchQueue.main.async {
            self.region = MKCoordinateRegion(center: latestLocation.coordinate, span: MKCoordinateSpan(latitudeDelta: 0.015, longitudeDelta: 0.015))
//check region value
            print("Region --> \(self.region)")
        }
    }
    
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print(error.localizedDescription)
    }
}

and this is the result in the terminal how can i get the latitude and longitude?

Region --> MKCoordinateRegion(center: __C.CLLocationCoordinate2D(latitude: 37.33036067, longitude: -122.02845007), span: __C.MKCoordinateSpan(latitudeDelta: 0.015, longitudeDelta: 0.015))

CodePudding user response:

You are already getting the values with your latest location, so all you need to do is to assign them to variables:

var latitude = region.center.latitude
var logitude = region.center.longitude

Hard for me to test, since can't just copy paste your code to try it out.

  • Related