Ios 如何在Swift 3/4中获取摄像机内的位置?
我遇到了一个问题,我有一个摄像头和一个地图视图,如果照片是使用应用程序拍摄的,我想做的就是获取位置,您可以在拍摄图像时访问用户的位置,并扩展UIImage以包含位置属性,同时将此位置保存到地图视图或tableView上当您使用手机摄像头拍摄照片时,请保存图像。谢谢你的帮助 这是我的密码。(摄像机控制器) MapViewControllerIos 如何在Swift 3/4中获取摄像机内的位置?,ios,swift,mapkit,core-location,Ios,Swift,Mapkit,Core Location,我遇到了一个问题,我有一个摄像头和一个地图视图,如果照片是使用应用程序拍摄的,我想做的就是获取位置,您可以在拍摄图像时访问用户的位置,并扩展UIImage以包含位置属性,同时将此位置保存到地图视图或tableView上当您使用手机摄像头拍摄照片时,请保存图像。谢谢你的帮助 这是我的密码。(摄像机控制器) MapViewController import Foundation import UIKit import MapKit class MapViewController: UIViewCo
import Foundation
import UIKit
import MapKit
class MapViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var tableView: UITableView!
let regionRadius: CLLocationDistance = 1000
var location: CLLocationManager!
let nearbyRequest = MKLocalSearchRequest()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
location = CLLocationManager()
location!.delegate = self
location.desiredAccuracy = kCLLocationAccuracyBest
location.requestAlwaysAuthorization()
location.startUpdatingLocation()
if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
location!.startUpdatingLocation()
} else {
location!.requestWhenInUseAuthorization()
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
mapView.showsUserLocation = true
}
override func viewWillDisappear(_ animated: Bool) {
mapView.showsUserLocation = false
}
func location(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
switch status {
case .notDetermined:
print("Not Determined")
case .restricted:
print("Restricted")
case .denied:
print("Denied")
case .authorizedAlways:
print("AuthorizedAlways")
case .authorizedWhenInUse:
print("AuthorizedWhenInUse")
location!.startUpdatingLocation()
}
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("Failed to initialize Map: ", error.description)
}
}
如果要在拍照时拍摄用户的位置,则需要在用户拍照时请求位置更新,而不仅仅是在其他
视图控制器中启动位置更新。您可以使用CLLocationManager().requestLocation()
请求一次位置更新,但请注意,这是一个异步调用,因此用户可能会在返回时完成拍照
为了缓解此问题,您可以使用PromiseKit
仅在异步方法完成时让函数返回,也可以使用CLLocationManagerDelegate
func locationManager(manager:CLLocationManager,didUpdateLocations位置:[CLLocation])
方法,并在收到位置更新后立即更新图片/地图视图
在MapViewController
changevar位置:CLLocationManager
到static var location=CLLocationManager()
,相应地更新MapViewController
类,以使用location
作为类型属性而不是隐式展开的实例属性。然后在CameraController
中更改您的功能,如下所示:
@IBAction func CamBtn(_ sender: Any) {
if !UIImagePickerController.isSourceTypeAvailable(.camera) {
return
}
let cameraPicker = UIImagePickerController()
cameraPicker.delegate = self
cameraPicker.sourceType = .camera
cameraPicker.allowsEditing = false
MapViewController.location.requestLocation()
guard let userLocation = MapViewController.location.location else {return}
present(cameraPicker, animated: true)
}
这不是一个完整的示例,您仍然需要将
userLocation
添加到图像/MapView中,但您应该能够自己找出这一部分。此外,在这个实现中,requestLocation()
不保证在您检查其值时返回,因此我宁愿使用PromiseKit
的CLLocationManager.promise()
函数,它只在位置更新后返回。如果您想在拍照时获取用户的位置,用户拍照时,您需要请求位置更新,而不仅仅是在其他视图控制器中启动位置更新。您可以使用CLLocationManager().requestLocation()
请求一次位置更新,但请注意,这是一个异步调用,因此用户可能会在返回时完成拍照
为了缓解此问题,您可以使用PromiseKit
仅在异步方法完成时让函数返回,也可以使用CLLocationManagerDelegate
func locationManager(manager:CLLocationManager,didUpdateLocations位置:[CLLocation])
方法,并在收到位置更新后立即更新图片/地图视图
在MapViewController
changevar位置:CLLocationManager
到static var location=CLLocationManager()
,相应地更新MapViewController
类,以使用location
作为类型属性而不是隐式展开的实例属性。然后在CameraController
中更改您的功能,如下所示:
@IBAction func CamBtn(_ sender: Any) {
if !UIImagePickerController.isSourceTypeAvailable(.camera) {
return
}
let cameraPicker = UIImagePickerController()
cameraPicker.delegate = self
cameraPicker.sourceType = .camera
cameraPicker.allowsEditing = false
MapViewController.location.requestLocation()
guard let userLocation = MapViewController.location.location else {return}
present(cameraPicker, animated: true)
}
这不是一个完整的示例,您仍然需要将
userLocation
添加到图像/MapView中,但您应该能够自己找出这一部分。此外,在这个实现中,requestLocation()
不保证在您检查其值时返回,因此我宁愿使用PromiseKit
的CLLocationManager.promise()
函数,它只在位置更新后返回。那么您的问题是什么,什么不起作用?请解释你面临的问题是什么,而不仅仅是你试图实现的目标。好吧,@DavidPasztor问题是,我得到了用户的位置,但当用户拍照时,它并没有完全得到。我想做的就是,当相机打开时,同时将用户的位置显示在地图视图或桌面视图上,什么时候被捕获?你有什么问题,什么不工作?请解释你面临的问题是什么,而不仅仅是你试图实现的目标。好吧,@DavidPasztor问题是,我得到了用户的位置,但当用户拍照时,它并没有完全得到。我所要做的就是,当相机打开时,在地图视图或桌面视图上同时获取用户的位置