日別アーカイブ: 2020年8月29日

100 Days of SwiftUI(DAY 78(Time for MapKitt))

ヒント:アプリを本当に便利にしたい場合sourceTypeは、イメージピッカーコントローラーのプロパティをに設定して、.cameraユーザーが既存の写真をインポートするのではなく、新しい写真を撮れるようにします。

ピンをドロップする方法とマップビューの中心座標を使用する方法を理解しているため、ユーザーの位置を取得してテキストと画像と共に保存する方法を理解するだけです。


import CoreLocation

class LocationFetcher: NSObject, CLLocationManagerDelegate {
    let manager = CLLocationManager()
    var lastKnownLocation: CLLocationCoordinate2D?

    override init() {
        super.init()
        manager.delegate = self
    }

    func start() {
        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        lastKnownLocation = locations.first?.coordinate
    }
}


struct ContentView: View {
    let locationFetcher = LocationFetcher()

    var body: some View {
        VStack {
            Button("Start Tracking Location") {
                self.locationFetcher.start()
            }

            Button("Read Location") {
                if let location = self.locationFetcher.lastKnownLocation {
                    print("Your location is \(location)")
                } else {
                    print("Your location is unknown")
                }
            }
        }
    }
}

関連↓
How to Use Camera and Library to Take Photos in SwiftUI