ios Swift 3 Google Map和MapKit API -如果用户的当前位置已更新

7vhp5slm  于 11个月前  发布在  iOS
关注(0)|答案(1)|浏览(110)

我正在尝试查看用户的位置何时更新。我尝试了许多不同的侦听器,但我似乎无法弄清楚这一点。测试用户位置是否已更新的最有效和高效的方法是什么?

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = locations[0]

    let lat = location.coordinate.latitude
    let long = location.coordinate.longitude
    print(lat, long)

    let currentLocation = CLLocationCoordinate2DMake(lat, long)
    let lastLocation = CLLocationCoordinate2DMake((locations.last?.coordinate.latitude)!, (locations.last?.coordinate.longitude)!)

    let camera = GMSCameraPosition.camera(withLatitude: lat, longitude: long, zoom: 15)
    self.mapView.animate(to: camera)

    let geocoder = GMSGeocoder()
        if currentLocation.latitude != lastLocation.latitude || currentLocation.longitude != lastLocation.longitude {
            let alertController = UIAlertController(title: "Location Update", message: "Current location has been updated", preferredStyle: .alert)

            let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
            alertController.addAction(defaultAction)

            self.present(alertController, animated: true, completion: nil)
        } else {
            print("no update change")
        }

    geocoder.reverseGeocodeCoordinate(currentLocation) { (response, error) in
        if let address = response?.firstResult() {
            let lines = address.lines!
            self.currentLocationLabel.text = lines.joined(separator: "\n")
        }
    }

    // self.manager.stopUpdatingLocation()
}

字符串

6mzjoqzu

6mzjoqzu1#

我想你只是想在Map上得到用户的位置,对吗?你可以从Map视图中得到用户的位置,你可以在didUpdateLocation中做到这一点

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

      if let mylocation = mapview.myLocation{

         //animate the mapview to user's location
         let lat = mylocation.coordinate.latitude
         let long = mylocation.coordinate.longitude
         let camera = GMSCameraPosition.camera(withLatitude: lat, longitude: long, zoom: 14.0)
         mapview.camera = camera
         mapview.animate(to: camera)

       }

}

字符串
希望对你有帮助。

相关问题