在IBAction函数中调用locationManager

时间:2019-04-14 22:47:28

标签: swift xcode locationmanager

我正在开发天气应用程序,我希望用户通过插入位置或当前的GPS位置来获取有关天气的数据。

对于GPS位置,界面中有一个可以按下的按钮。单击该按钮,我想调用locationManager函数,但是它不起作用。您认为这与示波器有关吗?

我只是将locationManager函数放入按钮的 IBAction 函数中。那是因为我不知道如何在IBAction中调用locationManager。

enter image description here

class ViewController: UIViewController, CLLocationManagerDelegate {

    let key = "d79ac3fea08fac5d21deeabef00*****"
    var lat = 11.344533
    var lon = 104.33322
    let managLoc = CLLocationManager()

    @IBAction func locationOption(_ sender: Any, forEvent event: UIEvent) {
        func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
            let location = locations[0]
            lat = location.coordinate.latitude
            lon = location.coordinate.longitude
            Alamofire.request("https://api.openweathermap.org/data/2.5/weather?lat=\(lat)&lon=\(lon)&appid=\(key)").responseJSON {
                response in
                if let responseStr = response.result.value {
                    let jsonResponse = JSON(responseStr)
                    let jsonWeather = jsonResponse["weather"].array![0]
                    let jsonTemp = jsonResponse["main"]
                    let iconName = jsonWeather["icon"].stringValue

                    self.locationLabel.text = jsonResponse["name"].stringValue
                    self.typeView.image = UIImage(named: iconName)
                    self.typeLabel.text = jsonWeather["main"].stringValue
                    self.tempLabel.text = "\(Int(round(jsonTemp["temp"].doubleValue-273.15)))"

                    let date = Date()
                    let dateFormatter = DateFormatter()
                    dateFormatter.dateFormat = "EEEE"
                    self.dayLabel.text = dateFormatter.string(from: date)

                    }
            }
            self.managLoc.stopUpdatingLocation()
        }

        func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
            print(error.localizedDescription)
        }
    }

1 个答案:

答案 0 :(得分:1)

从动作中获取locationManager函数

<?php
$old = array(
    'firstname',
    'bob',
    'lastName',
    'alex'
);

$new = array();

for($i = 0; $i < count($old); $i += 2) {
    $new[$old[$i]] = $old[$i + 1];
}

print_r($new);