mkmapview 注释只在平移两次后出现
Mkmapview annotations appear only after panning twice
平移或缩放到新区域时,我无法显示注释。如果我平移到一个新的地图区域,什么也不会出现,但如果我在那里做一个小平移,就会出现注释。我希望在用户不需要多次平移的情况下出现注释。
我能找到的类似问题似乎已经通过在主线程上加载注释来解决,但到目前为止,可能是不可取的,我运行一切都在主线程上。
我已经包含了我的 regionDidChange 和 regionWillChange 函数,我在其中注释了我的 mapView 和注释函数。
func mapView(_ mapView: MKMapView, regionWillChangeAnimated animated: Bool) {
zoomLevelBeforeChange = ((mapView.getZoomLevel() * 100).rounded() / 100)
}
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
var apiScale: String
print("Zoom: \(mapView.getZoomLevel())")
if mapView.getZoomLevel() < 5 {
mapView.setCenter(coordinate: mapView.centerCoordinate, zoomLevel: 5, animated: true)
apiScale = "6"
} else if mapView.getZoomLevel() >= 5.0 && mapView.getZoomLevel() < 6.0 {
apiScale = "6"
} else if mapView.getZoomLevel() >= 6.0 && mapView.getZoomLevel() < 7.5 {
apiScale = "7"
} else if mapView.getZoomLevel() >= 7.5 && mapView.getZoomLevel() < 8.5 {
apiScale = "9"
} else if mapView.getZoomLevel() >= 8.5 && mapView.getZoomLevel() <= 9.5 {
apiScale = "11"
} else if mapView.getZoomLevel() >= 9.5 && mapView.getZoomLevel() <= 10.0 {
apiScale = "13"
} else if mapView.getZoomLevel() > 10 {
mapView.setCenter(coordinate: mapView.centerCoordinate, zoomLevel: 10, animated: true)
apiScale = "13"
} else {
apiScale = "0"
}
print(apiScale)
if ((mapView.getZoomLevel() * 100).rounded() / 100) == zoomLevelBeforeChange {
print("don't remove annotations")
} else {
let allAnnotations = self.mapView.annotations
self.mapView.removeAnnotations(allAnnotations)
}
let latitudeDelta = mapView.region.span.latitudeDelta
let longitudeDelta = mapView.region.span.longitudeDelta
let centerCoordLat = mapView.centerCoordinate.latitude
let centerCoordLong = mapView.centerCoordinate.longitude
lowerLeftLong = (centerCoordLong - (longitudeDelta / 2))
lowerLeftLat = (centerCoordLat - (latitudeDelta / 2))
upperRightLong = (centerCoordLong + (longitudeDelta / 2))
upperRightLat = (centerCoordLat + (latitudeDelta / 2))
mapUrl = "http://api.openweathermap.org/data/2.5/box/city?bbox=\(lowerLeftLong!),\(lowerLeftLat!),\(upperRightLong!),\(upperRightLat!),\(apiScale)&appid=(appId)"
downloadMapWeatherApi {
annotate()
self.mapAnnotations = []
}
}
func downloadMapWeatherApi(completed: DownloadComplete) {
Alamofire.request(self.mapUrl).responseJSON { response in
let result = response.result
if let dict = result.value as? Dictionary<String, AnyObject> {
if let list = dict["list"] as? [Dictionary<String, AnyObject>] {
for obj in list {
let annotation = MapAnnotation(locationDict: obj)
self.mapAnnotations.append(annotation)
}
}
}
}
completed()
}
func annotate() {
for location in self.mapAnnotations {
let annotation = CustomAnnotation()
annotation.title = location.cityName
annotation.subtitle = "\(Int(location.temperature))°"
annotation.attribute = location.weatherType
annotation.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
mapView.addAnnotation(annotation)
}
}
谢谢!
一个非常简单的修复,但我花了很长时间才弄明白。我不知道背后的原因,但我的注释数组似乎始终来自最后一个 mapView 矩形。我将我的注释功能移动到我的天气数据 api 调用中并纠正了它。
func downloadMapWeatherApi(completed: DownloadComplete) {
Alamofire.request(self.mapUrl).responseJSON { response in
let result = response.result
if let dict = result.value as? Dictionary<String, AnyObject> {
if let list = dict["list"] as? [Dictionary<String, AnyObject>] {
for obj in list {
let annotation = MapAnnotation(locationDict: obj)
self.mapAnnotations.append(annotation)
}
self.annotate()
}
}
}
completed()
}
希望这可以帮助遇到类似问题的人。
平移或缩放到新区域时,我无法显示注释。如果我平移到一个新的地图区域,什么也不会出现,但如果我在那里做一个小平移,就会出现注释。我希望在用户不需要多次平移的情况下出现注释。
我能找到的类似问题似乎已经通过在主线程上加载注释来解决,但到目前为止,可能是不可取的,我运行一切都在主线程上。
我已经包含了我的 regionDidChange 和 regionWillChange 函数,我在其中注释了我的 mapView 和注释函数。
func mapView(_ mapView: MKMapView, regionWillChangeAnimated animated: Bool) {
zoomLevelBeforeChange = ((mapView.getZoomLevel() * 100).rounded() / 100)
}
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
var apiScale: String
print("Zoom: \(mapView.getZoomLevel())")
if mapView.getZoomLevel() < 5 {
mapView.setCenter(coordinate: mapView.centerCoordinate, zoomLevel: 5, animated: true)
apiScale = "6"
} else if mapView.getZoomLevel() >= 5.0 && mapView.getZoomLevel() < 6.0 {
apiScale = "6"
} else if mapView.getZoomLevel() >= 6.0 && mapView.getZoomLevel() < 7.5 {
apiScale = "7"
} else if mapView.getZoomLevel() >= 7.5 && mapView.getZoomLevel() < 8.5 {
apiScale = "9"
} else if mapView.getZoomLevel() >= 8.5 && mapView.getZoomLevel() <= 9.5 {
apiScale = "11"
} else if mapView.getZoomLevel() >= 9.5 && mapView.getZoomLevel() <= 10.0 {
apiScale = "13"
} else if mapView.getZoomLevel() > 10 {
mapView.setCenter(coordinate: mapView.centerCoordinate, zoomLevel: 10, animated: true)
apiScale = "13"
} else {
apiScale = "0"
}
print(apiScale)
if ((mapView.getZoomLevel() * 100).rounded() / 100) == zoomLevelBeforeChange {
print("don't remove annotations")
} else {
let allAnnotations = self.mapView.annotations
self.mapView.removeAnnotations(allAnnotations)
}
let latitudeDelta = mapView.region.span.latitudeDelta
let longitudeDelta = mapView.region.span.longitudeDelta
let centerCoordLat = mapView.centerCoordinate.latitude
let centerCoordLong = mapView.centerCoordinate.longitude
lowerLeftLong = (centerCoordLong - (longitudeDelta / 2))
lowerLeftLat = (centerCoordLat - (latitudeDelta / 2))
upperRightLong = (centerCoordLong + (longitudeDelta / 2))
upperRightLat = (centerCoordLat + (latitudeDelta / 2))
mapUrl = "http://api.openweathermap.org/data/2.5/box/city?bbox=\(lowerLeftLong!),\(lowerLeftLat!),\(upperRightLong!),\(upperRightLat!),\(apiScale)&appid=(appId)"
downloadMapWeatherApi {
annotate()
self.mapAnnotations = []
}
}
func downloadMapWeatherApi(completed: DownloadComplete) {
Alamofire.request(self.mapUrl).responseJSON { response in
let result = response.result
if let dict = result.value as? Dictionary<String, AnyObject> {
if let list = dict["list"] as? [Dictionary<String, AnyObject>] {
for obj in list {
let annotation = MapAnnotation(locationDict: obj)
self.mapAnnotations.append(annotation)
}
}
}
}
completed()
}
func annotate() {
for location in self.mapAnnotations {
let annotation = CustomAnnotation()
annotation.title = location.cityName
annotation.subtitle = "\(Int(location.temperature))°"
annotation.attribute = location.weatherType
annotation.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
mapView.addAnnotation(annotation)
}
}
谢谢!
一个非常简单的修复,但我花了很长时间才弄明白。我不知道背后的原因,但我的注释数组似乎始终来自最后一个 mapView 矩形。我将我的注释功能移动到我的天气数据 api 调用中并纠正了它。
func downloadMapWeatherApi(completed: DownloadComplete) {
Alamofire.request(self.mapUrl).responseJSON { response in
let result = response.result
if let dict = result.value as? Dictionary<String, AnyObject> {
if let list = dict["list"] as? [Dictionary<String, AnyObject>] {
for obj in list {
let annotation = MapAnnotation(locationDict: obj)
self.mapAnnotations.append(annotation)
}
self.annotate()
}
}
}
completed()
}
希望这可以帮助遇到类似问题的人。