在其他功能中使用位置管理器功能的值

时间:2021-08-20 19:42:05

I'm trying to extract latitude and longitude from the location manager function to then present in an UITableView. I want it to update the TableView as the location updates. But the Collect Array is still empty? How can I do this? Any input would be greatly appreciated!

我正在尝试从位置管理器函数中提取纬度和经度,然后在UITableView中显示。我希望它更新TableView作为位置更新。但收集阵列仍然是空的?我怎样才能做到这一点?任何投入将不胜感激!

Here's my code:

这是我的代码:

class ThirdViewController: UIViewController, CLLocationManagerDelegate, UITableViewDelegate, UITableViewDataSource{
    @IBOutlet weak var map: MKMapView!
    @IBOutlet var Tableview: UIView!

    let manager = CLLocationManager()
    var Collect = [Double]()

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let location = locations[0]
        let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01,0.01) //shows the size of map screen
        let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude,location.coordinate.longitude)
        let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
        map.setRegion(region, animated: true)
        self.map.showsUserLocation = true

        let lat = (location.coordinate.latitude)
        let long = (location.coordinate.longitude)
        Collect.append(lat)
        Collect.append(long)
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return Collect.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "TableCell")
            cell.textLabel?.text = Collect[indexPath.row]
        return cell
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()
        super.viewDidLoad()
        print(Collect)
    }

1 个解决方案

#1


1  

class ThirdViewController: UIViewController, CLLocationManagerDelegate, UITableViewDelegate, UITableViewDataSource{

@IBOutlet weak var map: MKMapView!
@IBOutlet var Tableview: UIView!

let manager = CLLocationManager()
var Collect = NSMutableArray()
var filterArr = NSArray()

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = locations[0]
    let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01,0.01) //shows the size of map screen
    let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude,location.coordinate.longitude)
    let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
    map.setRegion(region, animated: true)
    self.map.showsUserLocation = true


    let lat = String(location.coordinate.latitude)
    let long = String(location.coordinate.longitude)
    Collect.append(lat)
    Collect.append(long)
    filterArr = Collect as NSMutableArray()
    Tableview.reloadData()


}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return Collect.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "TableCell")
        cell.textLabel?.text = Collect[indexPath.row]
    return cell
}




override func viewDidLoad() {
    super.viewDidLoad()
    manager.requestWhenInUseAuthorization()
    manager.startUpdatingLocation()
    super.viewDidLoad()
    Tableview.delegate = self
    Tableview.datasource = self
    print(Collect)
}

#1


1  

class ThirdViewController: UIViewController, CLLocationManagerDelegate, UITableViewDelegate, UITableViewDataSource{

@IBOutlet weak var map: MKMapView!
@IBOutlet var Tableview: UIView!

let manager = CLLocationManager()
var Collect = NSMutableArray()
var filterArr = NSArray()

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = locations[0]
    let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01,0.01) //shows the size of map screen
    let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude,location.coordinate.longitude)
    let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
    map.setRegion(region, animated: true)
    self.map.showsUserLocation = true


    let lat = String(location.coordinate.latitude)
    let long = String(location.coordinate.longitude)
    Collect.append(lat)
    Collect.append(long)
    filterArr = Collect as NSMutableArray()
    Tableview.reloadData()


}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return Collect.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "TableCell")
        cell.textLabel?.text = Collect[indexPath.row]
    return cell
}




override func viewDidLoad() {
    super.viewDidLoad()
    manager.requestWhenInUseAuthorization()
    manager.startUpdatingLocation()
    super.viewDidLoad()
    Tableview.delegate = self
    Tableview.datasource = self
    print(Collect)
}

相关文章