gpt4 book ai didi

swift mapkit 当前位置

转载 作者:搜寻专家 更新时间:2023-11-01 06:58:26 25 4
gpt4 key购买 nike

我正在使用 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 函数完成后更新您的位置。

if distance 方法现在每 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
}
}
}

关于swift mapkit 当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52019380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com