如何存储用户的当前位置并在地图上显示位置?
我能够在地图上显示预定义的坐标,我只是不知道如何从设备接收信息.
另外我知道我必须在Plist中添加一些项目.我怎样才能做到这一点?
我想使用Swift获取位置的当前经度和纬度,并通过标签显示它们.我尝试这样做,但标签上没有显示任何内容.
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate{
@IBOutlet weak var longitude: UILabel!
@IBOutlet weak var latitude: UILabel!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
if (CLLocationManager.locationServicesEnabled()) {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
} else {
println("Location services are not enabled");
}
}
// MARK: - CoreLocation Delegate Methods
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
locationManager.stopUpdatingLocation()
removeLoadingView()
if ((error) != nil) {
print(error)
}
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
var …Run Code Online (Sandbox Code Playgroud) 我一直试图让它工作一段时间,我来这里问 - 如何在Swift中使用CLLocationManagerDelegate方法?我把它放在班上的顶端:
var locationManager = CLLocationManager()
Run Code Online (Sandbox Code Playgroud)
我把以下内容放入我的viewDidLoad方法中:
locationManager.delegate = self
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
Run Code Online (Sandbox Code Playgroud)
我尝试使用这些委托方法无济于事:
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: AnyObject[]!) {
locationReceived = true
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
locationReceived = false
}
Run Code Online (Sandbox Code Playgroud)
我也尝试在函数前使用@optional,但Xcode会抛出编译器错误.有任何想法吗?
我正在尝试创建一个简单的应用程序,以找出某人所在的区域,但是由于应用程序运行并找到位置时,不会调用任何CLLocationManagerDelegate方法,所以我陷入了困境。
如果相关,我也不会看到对话框,询问我是否允许该应用使用位置信息。
这是我到目前为止的内容-
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var locationLabel : UILabel!
var locationManager = CLLocationManager()
let geocoder = CLGeocoder ()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
override func viewDidDisappear(animated: Bool) {
locationManager.stopUpdatingLocation()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: …Run Code Online (Sandbox Code Playgroud) 如何使用地理位置元数据将照片保存到照片库?
我已请求(并允许)该应用访问用户位置:
private func allowAccessToUserLocation() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
}
Run Code Online (Sandbox Code Playgroud)
我是否需要询问相机应用程序的具体预设?
编辑:
我使用UIImagePickerController从应用程序中拍摄照片.从应用程序中拍摄的所有照片都存储在照片库中,没有地理位置.
这与我将图像保存到照片库的方式有什么关系吗?我的问题在这里而不是在核心位置权限?
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage{
UIImageWriteToSavedPhotosAlbum(pickedImage, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
dismiss(animated: true, completion: {
self.showPhoto()})
}
}
Run Code Online (Sandbox Code Playgroud)