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

标签 ios iphone swift api maps

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

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()
}

最佳答案

我想您只是想获取用户在 map 上的位置,对吧? 您可以从 map View 中获取用户的位置,您可以在 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)

       }

}

希望对您有所帮助。

关于ios - Swift 3 Google Map 和 MapKit API - 如果用户当前位置已更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44815056/

相关文章:

iphone - 调用协议(protocol)方法是否通过程序流控制?

ios - 如何跟踪 AVAudioFoundation 中的循环次数

iOS swift NSMutableData 没有成员 appendString

iphone - UITableView tableHeaderView 缺少分隔符

swift 2.2 : Optional Binding in a function

ios - 无法实例化函数 无法转换类型为 '__NSArrayI' 的值

iOS 的 UITableView 有问题以编程方式选择多个单元格并显示支票

iphone - 如何跨 View Controller 链接网点?

ios - 如何在我的 iOS 应用程序中使用 YouTube 数据 API?我在每个请求中收到 "Access not configured"错误

iphone - PickerView 仅返回我的数组中添加的最后一个对象