MKPinAnnotationView color is not working

 ̄綄美尐妖づ 提交于 2019-12-20 04:30:34

问题


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.

Why is that ?

 //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:


Regarding the main issue that the pins are red instead of green:

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.


Regarding the second issue that "it takes so much time to load":

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.

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.

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


来源:https://stackoverflow.com/questions/22127157/mkpinannotationview-color-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!