iOS Swift - 如何在 return 信息 Window 之前等待请求结束(Google 地图)
iOS Swift - How to wait end of request before return Info Window (Google Maps)
我正在使用 Google 地图和自定义信息 Window。
我遇到一个问题,我需要更新标签,但数据来自成功回调。
这里的问题是,当数据可用时,我已经返回了我的信息 Window 并且我无法再更新它,因为它已经呈现(视图的快照)。
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
let infoWindow = Bundle.main.loadNibNamed("CustomInfoWindow", owner: self, options: nil)?.first as! CustomInfoWindow
let obs = marker.userData as! Observation
ObsService.shared.getCityFromLatLong(lat: String(obs.coordinate.latitude), long: String(obs.coordinate.longitude)) { response in
infoWindow.outletPlaceLabel.text = response
}
infoWindow.setDateAndTime(timestamp: obs.timestamp)
return infoWindow
}
我知道我有几个选择,例如:
- 通知
- 等到请求完成(阻塞UI?)
- 安排我的任务
- 具有逻辑而不是仅 loadNibNamed 的 Init 方法
我开始开发 Swift 3.
编辑: 它正在使用 sleep(2)。但是每次我需要显示信息 window 时,我都无法阻止 UI。
我找不到其他解决方案。
试试这个:
let infoWindow:CustomInfoWindow? // declare out side of method
你的函数现在
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
infoWindow = Bundle.main.loadNibNamed("CustomInfoWindow", owner: self, options: nil)?.first as! CustomInfoWindow
let obs = marker.userData as! Observation
ObsService.shared.getCityFromLatLong(lat: String(obs.coordinate.latitude), long: String(obs.coordinate.longitude)) { response in
infoWindow.outletPlaceLabel.text = response
}
infoWindow.setDateAndTime(timestamp: obs.timestamp)
return infoWindow
}
var cityName: String?
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
if cityName == nil {
let obs = marker.userData as! Observation
ObsService.shared.getCityFromLatLong(lat: String(obs.coordinate.latitude), long: String(obs.coordinate.longitude) { response in
self.cityName = response
// Replace the existing map view with a new one here. Or call it's refresh function if it has one (I don't know, haven't used Google map view)
return
}
}
return self.mapViewHelper(mapView, markerInfoWindow: marker, city: cityName ?? "")
}
func mapViewHelper(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker, city: String?) -> UIView? {
let infoWindow = Bundle.main.loadNibNamed("CustomInfoWindow", owner: self, options: nil)?.first as! CustomInfoWindow
let obs = marker.userData as! Observation
infoWindow.outletPlaceLabel.text = response
infoWindow.setDateAndTime(timestamp: obs.timestamp)
return infoWindow
}
如果你最初使用Interface builder设置了地图视图,你可以给它一个标签,然后搜索带有相关标签的视图,在我写评论的地方实例化一个新的地图视图,并替换现有地图视图(从现有地图视图复制其框架)。地图视图也可能具有您可以调用的刷新功能。有关如何使用标签搜索视图的视图,请参阅文档(抱歉,如果那是在教祖母吸鸡蛋!)
我正在使用 Google 地图和自定义信息 Window。
我遇到一个问题,我需要更新标签,但数据来自成功回调。
这里的问题是,当数据可用时,我已经返回了我的信息 Window 并且我无法再更新它,因为它已经呈现(视图的快照)。
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
let infoWindow = Bundle.main.loadNibNamed("CustomInfoWindow", owner: self, options: nil)?.first as! CustomInfoWindow
let obs = marker.userData as! Observation
ObsService.shared.getCityFromLatLong(lat: String(obs.coordinate.latitude), long: String(obs.coordinate.longitude)) { response in
infoWindow.outletPlaceLabel.text = response
}
infoWindow.setDateAndTime(timestamp: obs.timestamp)
return infoWindow
}
我知道我有几个选择,例如:
- 通知
- 等到请求完成(阻塞UI?)
- 安排我的任务
- 具有逻辑而不是仅 loadNibNamed 的 Init 方法
我开始开发 Swift 3.
编辑: 它正在使用 sleep(2)。但是每次我需要显示信息 window 时,我都无法阻止 UI。
我找不到其他解决方案。
试试这个:
let infoWindow:CustomInfoWindow? // declare out side of method
你的函数现在
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
infoWindow = Bundle.main.loadNibNamed("CustomInfoWindow", owner: self, options: nil)?.first as! CustomInfoWindow
let obs = marker.userData as! Observation
ObsService.shared.getCityFromLatLong(lat: String(obs.coordinate.latitude), long: String(obs.coordinate.longitude)) { response in
infoWindow.outletPlaceLabel.text = response
}
infoWindow.setDateAndTime(timestamp: obs.timestamp)
return infoWindow
}
var cityName: String?
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
if cityName == nil {
let obs = marker.userData as! Observation
ObsService.shared.getCityFromLatLong(lat: String(obs.coordinate.latitude), long: String(obs.coordinate.longitude) { response in
self.cityName = response
// Replace the existing map view with a new one here. Or call it's refresh function if it has one (I don't know, haven't used Google map view)
return
}
}
return self.mapViewHelper(mapView, markerInfoWindow: marker, city: cityName ?? "")
}
func mapViewHelper(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker, city: String?) -> UIView? {
let infoWindow = Bundle.main.loadNibNamed("CustomInfoWindow", owner: self, options: nil)?.first as! CustomInfoWindow
let obs = marker.userData as! Observation
infoWindow.outletPlaceLabel.text = response
infoWindow.setDateAndTime(timestamp: obs.timestamp)
return infoWindow
}
如果你最初使用Interface builder设置了地图视图,你可以给它一个标签,然后搜索带有相关标签的视图,在我写评论的地方实例化一个新的地图视图,并替换现有地图视图(从现有地图视图复制其框架)。地图视图也可能具有您可以调用的刷新功能。有关如何使用标签搜索视图的视图,请参阅文档(抱歉,如果那是在教祖母吸鸡蛋!)