【问题标题】:Implementing forward geocoding function inside an iOS swiftui view在 iOS swiftui 视图中实现前向地理编码功能
【发布时间】:2020-04-23 00:55:02
【问题描述】:

所以我正在尝试使用 swiftui 制作一个 iOS 应用程序,该应用程序转发地址地理编码,然后将这些坐标放在可在我的其余视图中使用的变量中。我用于正向地理编码的函数如下所示:

import SwiftUI
import CoreLocation

struct ContentView: View {
    @State var myText = "Some Text just for reference"
    @State var location: CLLocationCoordinate2D?
    @State var lat: Double?
    @State var long: Double?

    var body: some View {
        VStack{
        Text(myText)
                  .onAppear {
                      self.getLocation(from: "22 Sunset Ave, East Quogue, NY") { coordinates in
                          print(coordinates ?? 0) // Print here
                          self.location = coordinates // Assign to a local variable for further processing
                        self.long = coordinates?.longitude
                        self.lat = coordinates?.latitude
                      }
              }
         Text("\(long)")
         Text("\(lat)")
        }

    }
      func getLocation(from address: String, completion: @escaping (_ location: CLLocationCoordinate2D?)-> Void) {
          let geocoder = CLGeocoder()
          geocoder.geocodeAddressString(address) { (placemarks, error) in
              guard let placemarks = placemarks,
          let location = placemarks.first?.location?.coordinate else {
              completion(nil)
              return
        }
          completion(location)
        }
    }
}

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

我的问题是,我应该如何在我的 contentview 文件中调用此函数,以便将坐标保存为变量,以及如何调用该函数将它们打印到屏幕上,以验证代码是否正常运行。

【问题讨论】:

    标签: ios swiftui core-location


    【解决方案1】:

    您可以在onAppear() 方法中以非常简单的方式执行此操作。但是,我鼓励您使用视图模型来使用地址获取坐标。

    struct ContentView: View {
        @State var myText = "Some Text just for reference"
        @State var location: CLLocationCoordinate2D?
        var body: some View {
            Text(myText)
                .onAppear {
                    self.getLocation(from: "Some Address") { coordinates in
                        print(coordinates) // Print here 
                        self.location = coordinates // Assign to a local variable for further processing
                    }
            }
        }
    
        func getLocation(from address: String, completion: @escaping (_ location: CLLocationCoordinate2D?)-> Void) {
            let geocoder = CLGeocoder()
            geocoder.geocodeAddressString(address) { (placemarks, error) in
                guard let placemarks = placemarks,
                let location = placemarks.first?.location?.coordinate else {
                    completion(nil)
                    return
                }
                completion(location)
            }
        }
    }
    

    【讨论】:

    • 在实现此方法时,会出现以下错误:函数声明了一个不透明的返回类型,但在其主体中没有用于推断基础类型的返回语句。还有警告:调用“onAppear(perform:)”的结果未使用,正在显示。有关如何解决这些问题的任何建议?
    • @TJD'Alessandro 您是否像我在示例中那样使用 onAppear?
    • 我将这段代码复制到一个全新的项目中,运行时出现了我提到的错误
    • @TJD'Alessandro 你能在聊天室分享你的代码吗? chat.stackoverflow.com/rooms/212370/swiftui-geocoding
    • 我无法在聊天中发帖,因为我的声望低于 20。我项目中的代码正是你在答案中写的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-02
    相关资源
    最近更新 更多