MKPinAnnotationView颜色不起作用

时间:2014-03-02 11:50:02

标签: ios objective-c mkmapview mkpinannotationview

我试图从数组中显示一些引脚,它会显示所有引脚,但它们是红色的,而不是绿色,因为我要求它们。

为什么?

 //run on array to get all locations
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";
        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;
}

1 个答案:

答案 0 :(得分:0)

关于引脚为红色而非绿色的主要问题:

代码会创建MKPinAnnotationView,但此视图从未提供给地图视图
要使地图视图使用您创建的注释视图 ,您必须实施viewForAnnotation委托方法并从那里返回
否则,地图视图不了解您创建的注释视图 如果您未实现viewForAnnotation,则地图视图会创建默认的红色引脚视图。


关于“需要花费很多时间才能加载”的第二个问题:

最可能的原因是您在每次时添加注释时都会调用setRegion 。 如果您要添加500个注释,则地图视图会将该区域设置为500次 请注意, 无需仅为了添加注释 而致电setRegion(无论当前可见区域如何)。注释的坐标必须“可见”才能在那里添加注释。

您想在for循环中执行的操作只是构造包含所有注释的区域,然后调用setRegion(或setVisibleRect)< em>一次之后添加所有注释(在for循环之后)。构建MKMapRect并调用setVisibleMapRect比构建MKCoordinateRegion以便调用setRegion更容易。

在iOS 7中,这更简单:只需拨打showAnnotations(无需手动构建)。


例如:

//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;
}