【问题标题】:enable and disable location using UISwitch in Swift 4在 Swift 4 中使用 UISwitch 启用和禁用位置
【发布时间】:2018-05-10 11:19:48
【问题描述】:

我正在研究谷歌地图,我想使用UISwitch 在谷歌地图上显示位置。我可以使用谷歌地图按钮显示位置,但我不知道如何使用UISwitch。正如您在我的第一张图片中看到的那样,我能够获得位置,但我怎样才能通过使用UISwitch(我正在使用MFSideMenu)显示侧边菜单来做到这一点。有什么帮助吗?

         override func viewDidLoad() {
    super.viewDidLoad()


    setupSwitch()

    locManager = CLLocationManager()
    locManager.delegate = self
    locManager.requestWhenInUseAuthorization()
    locManager.startMonitoringSignificantLocationChanges()

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print("Error while getting location\(error)")
    }



}

func setupSwitch() {
    locationSwitch.addTarget(self, action: #selector(switchValueDidChange(_:)), for: .valueChanged)
}
        @objc func switchValueDidChange(_ sender: UISwitch) {
    //Called when yourSwitch change its value
    if locationSwitch.isOn {
         menuContainerViewController.toggleLeftSideMenuCompletion({
    //self.locManager.startUpdatingLocation()

              })
    } else {
        //Stops the location updates.
        self.locManager.stopUpdatingLocation()
    }
}

   private func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    let location = locations.last

    _ = GMSCameraPosition.camera(withLatitude: (location?.coordinate.latitude)!, longitude: (location?.coordinate.longitude)!, zoom: 17.0)


    //Finally stop updating location otherwise it will come again and again in this delegate
    self.locManager.stopUpdatingLocation()

}

这是我写的代码,但它似乎不起作用

【问题讨论】:

    标签: ios swift google-maps navigation swift4


    【解决方案1】:

    UISwitch 是一个UIControl 对象,正如苹果的文档所说:

    控件使用目标-动作机制来报告有趣的事件 发生在您的代码上。

    你需要使用的是方法addTarget(_:action:for:)和使用事件valueChanged

    在您的情况下,您的代码可能如下所示:

         func setupSwitch {
           yourSwitch.addTarget(self, action: #selector(switchValueDidChange(_:)), for: .valueChanged)
         }
    
         func switchValueDidChange(_ sender: UISwitch) {
             //Called when yourSwitch change its value
            if locationSwitch.isOn {
                 //Starts tracking the user's current location.
                 self.locManager.startUpdatingLocation()
            } else {
                 //Stops the location updates.
                 self.locManager.stopUpdatingLocation()
            }      
         }
    

    您还可以从 .xib 文件中设置valueChanged 操作。

    【讨论】:

    • 但是先生,我想在主屏幕上更新我的位置。如果我使用 addTarget 方法而不是插座,这个答案是否会更新我在谷歌地图上的位置
    • 好吧,我对你的问题有点困惑。正如您所说的“我能够获得位置”,我认为您的问题只是 UISwitch 类。如果你想在谷歌地图中显示你的当前位置,你需要关注这个code
    • 我的问题是如何在开关打开并且我上面写的代码不起作用时显示当前位置
    • 好的,但我认为您有足够的信息来解决您的问题。我发布的代码将使您能够在开关打开/关闭时执行代码(在我的示例中,如果开关打开,它将开始获取用户位置)并且我在上一个代码中添加的链接将允许您在地图中显示用户的当前位置。
    • 这是另一个问题,需要通过重构代码来解决。
    猜你喜欢
    • 2021-04-06
    • 2018-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多