简体   繁体   English

如何获取经度和纬度然后将其存储在变量中? 斯威夫特

[英]How to get the Longitude and Latitude then store it on a Variable? on Swift

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))

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.我很难测试,因为不能只是复制粘贴您的代码来尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM