Ios 为什么不调用ViewForAnnotation?
我知道这个问题已经被问了很多次,但是所有的答案似乎都和我的应用程序中的情况略有不同 我的理解是,一旦mapView将其委托设置为显示它的ViewController,并且在mapView滚动时将注释添加到地图中,viewForAnnotation函数将被调用,以便它显示在mapView区域内 目前,我有一个主viewController(mainVC),其中包含一个MKMapView(mapView) 此viewController控制要在mapView中显示的四种不同地图Ios 为什么不调用ViewForAnnotation?,ios,swift2,mkmapview,mkannotationview,Ios,Swift2,Mkmapview,Mkannotationview,我知道这个问题已经被问了很多次,但是所有的答案似乎都和我的应用程序中的情况略有不同 我的理解是,一旦mapView将其委托设置为显示它的ViewController,并且在mapView滚动时将注释添加到地图中,viewForAnnotation函数将被调用,以便它显示在mapView区域内 目前,我有一个主viewController(mainVC),其中包含一个MKMapView(mapView) 此viewController控制要在mapView中显示的四种不同地图 func moveV
func moveViews(sender:Int) {
// This function handles which button on the Segmented Control was clicked and the loads the appropriate map into the mapView (passed as a para
removeAnnotationsAndOverlays() // ~~~
if sender == 0 {
// First Map was selected
let map1VC = map1VC()
map1VC.loadMap1View(mapView)
map1VC.centerMapOnLocation()
}
else if sender == 1 {
// Second Map was selected
let map2VC = map2VC()
map2VC.loadMap2View(mapView)
}
else if sender == 2 {
// Third Map was selected
let map3VC = map3VC()
map3VC.loadMap3View(mapView)
}
else if sender == 3 {
// Fourth Map was selected
let map4VC = map4VC()
map4VC.loadMap4View(mapView)
}
else {
// Load First Map as default
let map1VC = map1VC()
map1VC.loadMap1View(mapView)
map1VC.centerMapOnLocation()
}
}
有几个不同的类控制每个不同地图的功能:
- 正在正确加载MKMapView
}var mapView: MKMapView = MKMapView() // declared as a global variable/object inside the map4VC() // Initial function to set up Map // Think of this function as the "override func viewDidLoad() {}" func loadMap4View(mV: MKMapView) { // This connects the parameter passed (mV) and sets it as the delegate for the mapView used throughout this file // In other words, it allows the mapView on the MainVC to use all of the code in this file to handle different actions mapView = mV mapView.delegate = self let initialLocation = CLLocation(latitude: 50.3603125, longitude: 2.794017) // calculates the region you'll look at on the screen let coordinateRegion = MKCoordinateRegionMakeWithDistance(initialLocation.coordinate, regionRadius, regionRadius) // sets the region of the map mapView.setRegion(coordinateRegion, animated: true) //addPins() // This can use a custom function to load all of the Pins //mapView.addAnnotations(coords.allLocations!) // This line also works to add all of the individual pins mapView.addAnnotation(coords.allLocations![2]) // This adds one single Pin
- 将mapView的委托设置为当前类(mapView.delegate=self)
- 它在适当的位置放大(mapView.setRegion(coordinateRegion,动画:true))
- 该类从plist读取数据,并(使用helper类)构建自定义mkannotation(CemAnno)数组
- 这些位置存储在一个名为allLocations的CemAnno数组中:
}var allLocations: [CemAnno]? = [] // This is declared in the helper class along with a bunch of other stuff that grabs all of the information from a plist class CemAnno: NSObject, MKAnnotation { var coordinate: CLLocationCoordinate2D var title: String? var casualties: String? var visitInfo: String? var histInfo: String? var region: String? init(title: String, coordinate: CLLocationCoordinate2D, region: String, casualties: String, visitInfo: String, histInfo: String) { self.coordinate = coordinate self.title = title self.region = region self.casualties = casualties self.visitInfo = visitInfo self.histInfo = histInfo }
- 将它们中的每一个添加到地图(mapView.addAnnotations),并显示为接点(正在显示) mapView.addAnnotation(coords.allLocations![2])//这会添加一个单针注释(可以工作),但不会调用viewForAnnotation函数
// This function is NEVER called
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?
{
// Define a reuse identifier. This is a string that will be used to ensure we reuse annotation views as much as possible.
let identifier = "CemAnno"
// Check whether the annotation we're creating a view for is one of our CemAnno objects.
if annotation.isKindOfClass(CemAnno.self) {
print("correct class")
//let anno = annotation as! CustomAnnotation
// Try to dequeue an annotation view from the map view's pool of unused views.
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
if annotationView == nil {
print("no reusable view")
// If it isn't able to find a reusable view, create a new one using MKPinAnnotationView and sets its canShowCallout property to be true. This triggers the popup with the name.
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView!.canShowCallout = true
// Create a new UIButton using the built-in .Custom type. This is so we can add an image to the button.
let btn = UIButton(type: .DetailDisclosure)
//btn.setImage(anno.image, forState: .Normal)
annotationView!.rightCalloutAccessoryView = btn
} else {
print("reusing a view")
// If it can reuse a view, update that view to use a different annotation.
annotationView!.annotation = annotation
}
return annotationView
}
// If the annotation isn't from a CustomClass, it must return nil so iOS uses a default view.
return MKPinAnnotationView()
}
我已经尝试在地图的当前视图区域中添加一个pin的位置,但是ViewForAnnotation从未触发。
我已经尝试在地图的当前视图区域之外添加pin的位置,但是ViewForAnnotation从未被激活-我想这应该是一个有效的方法,当我“滚动”地图时,它会显示在当前视图区域内,应该激活该功能,但它没有(我应该注意,pin正在显示并显示在地图上)
这使得我无法自定义要执行的pin
我使用了与Map1相同的技术,但由于某些原因,ViewForAnnotation在Map4中从未被调用
如果您有任何建议,我们将不胜感激!!对不起,我看不到您的视图控制器mainVC的声明:您的控制器实现了
MKMapViewDelegate
import MapKit
class mainVC: UIViewController, MKMapViewDelegate
{
...
}
编辑1:如果代理与您的控制器链接如下,请检查脚本上的视图控制器中是否有代理:
只需在地图视图上单击鼠标右键。我不时遇到此问题。我发现有时您需要手动调用
showAnnotations:animated:
,如下所示:
mapView.showAnnotations(mapView.annotations, animated: true)
<>这是一个非常愚蠢的答案,但今天我有两次这样做:检查你的纬度/经度。如果你交换了它,你就有一个无效的点,或者一个在海洋的中间,所以这个视图永远不会被请求,因为它很可能在你可见的外部之外。一些代码可能会帮助我添加代码。rom Map1到Map4返回Map1只有一些时候我的自定义注释会显示我想要的图像。其他时候它们只显示一个pin。我已经玩了很多东西,我认为这一定与委托有关。虽然委托被设置,但似乎没有正确设置为instanc正在显示的MKMapView的e(尽管在func loadMap4View(mV:MKMapView)方法的第一行中进行了设置)。它是否与传递给单独类(通过引用或类型传递)的mV参数有关??有什么想法吗?奇怪的功能:在MainVC上,通过使用分段控件选择不同的贴图。当我快速按下不同的按钮加载不同的贴图时,大约有10%的时间程序会中断并点击ViewForAnnotation方法内的断点(这就是我希望发生的事情)。为什么只有10%的时间?在某些情况下,我无法复制它,它似乎完全是随机的。如果您在AsyncQueue中添加批注,请在mainQueue中添加批注。是的,它会添加批注。所有其他类也是如此。委托是在代码中设置的(如上所述)。考虑到此解决方案没有解释为什么它在某些时间工作而在其他时间不工作,我很惊讶此解决方案收到了UPVots。有四个不同的类正在处理在MainVC上加载mapView。因此,我不希望委托成为主视图控制器,因为代码包含在其他视图控制器类中f单个地图。它的工作稍微好一点,但仍然有相当多的问题。ViewForAnnotation几乎是随机触发的,有时图像结果而不是pin注释,而其他时间则不是如此。@M.Black问题可能出现在“mapView=mV”中。更合适的方法是:1.删除旧的mapView,2.设置新的mapview的框架与旧的mapview相同,3.添加新的mapview这是最接近工作的解决方案,尽管它似乎仍在随机调用ViewForAnnotation方法,并且仅在某些时候加载适当的图像。抱歉wj2061,赏金没有授予您(我不确定为什么授予其他解决方案??)
mapView.showAnnotations(mapView.annotations, animated: true)