本文介绍了MKPinAnnotationView颜色不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我试图从数组中显示一些引脚,它显示它们全部但它们是红色的,而不是绿色,因为我要求它们。I am trying to show some pins from an array, it shows them all but they are red, and not green as i ask them to be.为什么是那个? //run on array to get all locationsfor(int k=0;k<[array count];k=k+2){ float targetlat=[[array objectAtIndex:k] floatValue]; float targetlongi=[[array objectAtIndex:k+1] floatValue]; CLLocationCoordinate2D location = CLLocationCoordinate2DMake(targetlat,targetlongi); NSString *partyTitle = @"title"; MKPinAnnotationView *partyPin = [self returnPointView:location andTitle:partyTitle andColor:MKPinAnnotationColorGreen]; [self.mapView addAnnotation:partyPin.annotation];} //function callback is working but its red, and it takes so much time to load-(MKPinAnnotationView*) returnPointView: (CLLocationCoordinate2D) location andTitle: (NSString*) title andColor: (int) color{ MKCoordinateRegion region = self.mapView.region; region.center = location; region.span.longitudeDelta /= 5.0; region.span.latitudeDelta /= 5.0; [self.mapView setRegion:region]; MKPointAnnotation *resultPin = [[MKPointAnnotation alloc] init]; MKPinAnnotationView *result = [[MKPinAnnotationView alloc] initWithAnnotation:resultPin reuseIdentifier:Nil]; [resultPin setCoordinate:location]; resultPin.title = title; result.pinColor = color; return result;}推荐答案 关于主要问题是引脚是红色而不是绿色:代码创建一个 MKPinAnnotationView 但这个视图从未被提供给地图视图。 要使地图视图使用您创建的注释视图, 必须实现 viewForAnnotation 委托方法并从那里返回 。 否则,地图视图不知道您创建的注释视图。 如果你没有实现 viewForAnnotation ,地图视图会创建一个默认的红色引脚视图。The code creates an MKPinAnnotationView but this view is never given to the map view.To make the map view use annotation views that you create, you must implement the viewForAnnotation delegate method and return them from there.Otherwise, the map view has no knowledge of annotation views that you create.If you don't implement viewForAnnotation, the map view creates a default red pin view. 关于需要花费很多时间才能加载的第二个问题:最可能的原因是您要添加注释的时间是 setRegion 每次时间。 如果您要添加500个注释,地图视图正在设置区域500次。 请注意 无需调用 setRegion 只需添加注释 (无论当前可见区域)。注释的坐标不必须可见才能在那里添加注释。The most likely reason for this is that you are calling setRegion each time you add an annotation.If you are adding, say, 500 annotations, the map view is setting the region 500 times.Please note that it is not necessary to call setRegion simply to add an annotation (regardless of the currently-visible region). The annotation's coordinate does not have to be "visible" to add an annotation there.你想在 for loop只是 construct 一个包含所有注释的区域,然后调用 setRegion (或 setVisibleRect )一次, 后添加所有注释( 循环)。构造 MKMapRect 并调用 setVisibleMapRect 比构建 MKCoordinateRegion 为了拨打 setRegion 。What you want to do inside the for loop is simply construct a region that includes all the annotations and then call setRegion (or setVisibleRect) once and after all the annotations are added (after the for loop). Constructing an MKMapRect and calling setVisibleMapRect is easier than constructing an MKCoordinateRegion in order to call setRegion.在iOS 7中,这更简单:只需拨打 showAnnotations (无需手动构造)。In iOS 7, this is even simpler: Just call showAnnotations (no manual construction necessary). 示例:Example://Initialize the MKMapRect (region) we want to show to null...MKMapRect showMapRect = MKMapRectNull;for(int k=0;k<[array count];k=k+2){ float targetlat=[[array objectAtIndex:k] floatValue]; float targetlongi=[[array objectAtIndex:k+1] floatValue]; CLLocationCoordinate2D location = CLLocationCoordinate2DMake(targetlat,targetlongi); NSString *partyTitle = @"title"; //Here, don't create the annotation view. //Just create the annotation... MKPointAnnotation *resultPin = [[MKPointAnnotation alloc] init]; [resultPin setCoordinate:location]; resultPin.title = partyTitle; [self.mapView addAnnotation:resultPin]; //Add this annotation's coordinate //to the MKMapRect we want to show... MKMapPoint annMapPoint = MKMapPointForCoordinate(location); MKMapRect annMapRect = MKMapRectMake(annMapPoint.x, annMapPoint.y, 0, 0); showMapRect = MKMapRectUnion(showMapRect, annMapRect);}mapView.visibleMapRect = showMapRect;//In iOS 7, instead of constructing MKMapRect manually,//we could just call showAnnotations...//[mapView showAnnotations:mapView.annotations animated:YES];//Implement the viewForAnnotation delegate method...-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{ //if annotation is the user location, //return nil so map view shows default view for it (blue dot)... if ([annotation isKindOfClass:[MKUserLocation class]]) { return nil; } static NSString *reuseId = @"pin"; MKPinAnnotationView *pav = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseId]; if (pav == nil) { pav = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId]; pav.canShowCallout = YES; pav.animatesDrop = YES; pav.pinColor = MKPinAnnotationColorGreen; } else { pav.annotation = annotation; } return pav;} 这篇关于MKPinAnnotationView颜色不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-03 23:39