MKPinAnnotationView: Are there more than three colors available?

前端 未结 9 1265

According to the Apple docs, MKPinAnnotationView\'s pin color is available in red, green and purple. Is there any way to get other colors also? I\'ve found nothing in the do

相关标签:
9条回答
  • 2020-11-27 10:33

    I like Yonel's Answer but just a heads up, when you create a custom MKAnnotationView, you'll have to manually assign the offset. For the images Yonel provided: (you can leave out the calloutButton stuff if you don't need one of those)

    #pragma mark MKMapViewDelegate
    - (MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id <MKAnnotation>)annotation
    {
        if(![annotation isKindOfClass:[MyAnnotation class]]) // Don't mess user location
            return nil;
    
        MKAnnotationView *annotationView = [aMapView dequeueReusableAnnotationViewWithIdentifier:@"spot"];
        if(!annotationView)
        {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"spot"];
            annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            [(UIButton *)annotationView.rightCalloutAccessoryView addTarget:self action:@selector(openSpot:) forControlEvents:UIControlEventTouchUpInside];
            annotationView.enabled = YES;
            annotationView.canShowCallout = YES;
            annotationView.centerOffset = CGPointMake(7,-15);
            annotationView.calloutOffset = CGPointMake(-8,0);
        }
    
        // Setup annotation view
        annotationView.image = [UIImage imageNamed:@"pinYellow.png"]; // Or whatever
    
        return annotationView;
    }
    
    0 讨论(0)
  • 2020-11-27 10:34

    If it's not in the docs then most probably not, you cAn use mkannotationview and have ur own image if u wish though

    0 讨论(0)
  • 2020-11-27 10:38

    some more ;)

    enter image description hereenter image description here

    enter image description hereenter image description here

    And the original ones :

    alt text enter image description here

    alt text enter image description here

    alt text enter image description here

    And the code:

    - (MKAnnotationView*)mapView:(MKMapView*)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    MKPinAnnotationView* anView =[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"test"];
    anView.pinColor=MKPinAnnotationColorPurple;
    UIImage* image = nil;
    // 2.0 is for retina. Use 3.0 for iPhone6+, 1.0 for "classic" res.
    UIGraphicsBeginImageContextWithOptions(anView.frame.size, NO, 2.0);
    [anView.layer renderInContext: UIGraphicsGetCurrentContext()];
    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData* imgData = UIImagePNGRepresentation(image);
    NSString* targetPath = [NSString stringWithFormat:@"%@/%@", [self writablePath], @"thisismypin.png" ];
    [imgData writeToFile:targetPath atomically:YES]; 
    return anView;
    }
    
    -(NSString*) writablePath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return documentsDirectory;
    }
    
    0 讨论(0)
提交回复
热议问题