【问题标题】:Swift 3 Google Map and MapKit API - if users current location has been updatedSwift 3 Google Map 和 MapKit API - 如果用户当前位置已更新
【发布时间】:2017-06-29 01:42:43
【问题描述】:

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

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

【问题讨论】:

  • didUpdateLocations 对您不起作用?
  • 这是布尔值吗?如果 didUpdateLocations 我可以在哪里写

标签: ios iphone swift api maps


【解决方案1】:

我认为您只是想在地图上获取用户的位置,对吗? 您可以从地图视图中获取用户的位置,您可以在 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)

       }

}

希望对你有帮助。

【讨论】:

  • 这样每次用户位置变化时都会更新用户位置?
  • 是的。当用户的位置显着更新时,它也会更新地图视图
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多