我正在从服务器加载一组自定义注释,并希望使用以下代码将它们添加到地图中。当应用程序位于前台时,注释仅在10秒左右时出现在地图上。但是,当我进入主屏幕(应用程序在后台)然后返回应用程序时,它们会立即出现。 当应用程序位于前台时,如何能够立即显示这些注释?
我从服务器获取自定义注释数组,并尝试将它们添加到地图中,如下所示:
if UIApplication.sharedApplication().applicationState == .Active {
self.myMap.showAnnotations(self.these_new_pins, animated: true)
}
我的mapView viewForAnnotation如下所示:
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if !(annotation is CustomPointAnnotation) {
return nil
}
// ... otherwise continue executing function
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier("pin")
if pinView == nil {
pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin")
pinView!.canShowCallout = true
pinView!.alpha = 0.97
pinView!.centerOffset.y = -23
}
else {
pinView!.annotation = annotation
}
let cpa = annotation as! CustomPointAnnotation
// Pin color varies (user green, server blue)
pinView!.image = UIImage(named: cpa.pin_image)
// Add detail button to right callout
var calloutButton = UIButton.buttonWithType(.System) as! UIButton
var btnimage = UIImage(named: cpa.pin_btnimage)!
calloutButton.frame = CGRectMake(0, 0, 30, 30)
calloutButton.setBackgroundImage(btnimage, forState: UIControlState.Normal)
pinView!.rightCalloutAccessoryView = calloutButton
// Add detail button to left callout
var optionsButton = UIButton.buttonWithType(.System) as! UIButton
var btnimage_options = UIImage(named: cpa.pin_btnimage_options)
optionsButton.frame = CGRectMake(0, 0, 30, 30)
optionsButton.setBackgroundImage(btnimage_options, forState: UIControlState.Normal)
pinView!.leftCalloutAccessoryView = optionsButton
return pinView
}
谢谢!
答案 0 :(得分:0)
答案是从主线程调用showAnnotations(感谢Mozilla)。这是代码:
dispatch_async(dispatch_get_main_queue()) { if UIApplication.sharedApplication().applicationState == .Active { self.myMap.showAnnotations(self.these_new_pins, animated: true) } }