swift地图套件当前位置
我正在使用swift地图套件当前位置,swift,mapkit,Swift,Mapkit,我正在使用MapKit获取当前位置,但它始终没有获取任何位置 import UIKit import MapKit class NewLocationController: UIViewController,CLLocationManagerDelegate { @IBOutlet weak var nameTextField: UITextField! @IBOutlet weak var descriptionTextField: UITextField! @IB
MapKit
获取当前位置,但它始终没有获取任何位置
import UIKit
import MapKit
class NewLocationController: UIViewController,CLLocationManagerDelegate {
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var descriptionTextField: UITextField!
@IBOutlet weak var latitudeTextField: UITextField!
@IBOutlet weak var longtitudeTextField: UITextField!
var delegate: newLocationDelegate?
var locationManager: CLLocationManager = CLLocationManager()
var currentLocation: CLLocationCoordinate2D?
override func viewDidLoad() {
super.viewDidLoad()
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.distanceFilter = 10
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
// Do any additional setup after loading the view.
}
func locationManager (_ manager: CLLocationManager, didUpdateLocations locations:[CLLocation]){
let loc: CLLocation = locations.last!
currentLocation = loc.coordinate
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func addNewLocation(title: String, description: String, lat: Double, long: Double){
let location : FencedAnnotation = FencedAnnotation(newTitle: title, newSubtitle: description, lat: lat, long: long)
delegate!.didSaveLocation(location)
self.navigationController?.popViewController(animated: true)
}
@IBAction func saveNewAnimal(_ sender: Any) {
addNewLocation(title: nameTextField.text!, description: descriptionTextField.text!, lat:Double(latitudeTextField.text!)!, long:Double(longtitudeTextField.text!)!)
}
@IBAction func saveCurrentLocation(_ sender: Any) {
self.latitudeTextField.text = "\(currentLocation!.latitude)"
self.longtitudeTextField.text = "\(currentLocation!.longitude)"
}
尝试导入CoreLocation,调用
viewDidLoad
中的mLocation
。
LocationManager在完成mLocation功能后更新您的位置
如果距离
方法现在每100米更新一次
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
let myLocation = CLLocation()
let locationManager = CLLocationManager()
@IBOutlet weak var latitudeTextField: UITextField!
@IBOutlet weak var longtitudeTextField: UITextField!
override func viewDidLoad(){
super.viewDidLoad()
mLocation()
}
@IBAction func saveCurrentLocation(){
self.latitudeTextField.text = "\\(myLocation.coordinate.latitude)"
self.longtitudeTextField.text = "\(myLocation.coordinate.longitude)"
}
func mLocation(){
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled(){
locationManager.startUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations:[CLLocation]){
let newLocation = locations[0]
let distance = myLocation.distance(from: newLocation)
if distance > 100 {
myLocation = newLocation
}
}
}
当我单击savecurrentlocation时,“currentLocation”为零。您可以在接收位置数据的位置放置一个
打印
?你有输出吗?请正确缩进代码,谢谢。