2

我有以下文件:

注释.h:

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface annotation : NSObject <MKAnnotation>

@property(nonatomic, assign) CLLocationCoordinate2D coordinate;
@property(nonatomic, copy) NSString *title;
@property(nonatomic, copy) NSString *subtitle;


@end

注释.m:

#import "annotation.h"

@implementation annotation

@synthesize coordinate, title, subtitle;

@end

在主代码中,它包含NSURL在其他地方找到的:

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[spinner stopAnimating];

// json parsing
results = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];

NSMutableArray * locations = [[NSMutableArray alloc] init];
CLLocationCoordinate2D location;
annotation * myAnn;

NSArray *pins = mapView.annotations;

if ([pins count])
{
    [mapView removeAnnotations:pins];
}

/* loop through the array, each key in the array has a JSON String with format:
 * title <- string
 * strap <- string
 * id <- int
 * date <- date
 * lat <- floating point double
 * long <- floating point double
 * link <- string

 */
int i;

for (i = 0; i < [results count]; i++) {
    //NSLog(@"Result: %i = %@", i, results[i]);
    //NSLog(@"%@",[[results objectAtIndex:i] objectForKey:@"long"]);

    myAnn = [[annotation alloc] init];
    location.latitude = (double)[[[results objectAtIndex:i] objectForKey:@"lat"] doubleValue];
    location.longitude = (double)[[[results objectAtIndex:i] objectForKey:@"long"] doubleValue];
    myAnn.coordinate = location;
    myAnn.title = [[results objectAtIndex:i] objectForKey:@"title"];
    myAnn.subtitle = [[results objectAtIndex:i] objectForKey:@"strap"];

    [locations addObject:myAnn];

    //NSLog(@"%i", [[results objectAtIndex:i] objectForKey:@"lat"]);
}

[self.mapView addAnnotations:locations];

我之前看过的东西说我需要使用MKAnnotationView而不是MKPinAnnotationView但你可以看到我不使用任何一个,我是否可以为放置在屏幕上的图钉使用自定义图像。

4

3 回答 3

5

您 (a) 确保将您的视图控制器定义为delegate适合您的MKMapView; (b) 实施viewForAnnotation,例如:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    if ([annotation isKindOfClass:[MKUserLocation class]]) {
        return nil;
    }

    if ([annotation isKindOfClass:[CustomAnnotation class]]) {
        static NSString * const identifier = @"MyCustomAnnotation";
                    
        MKAnnotationView* annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
        
        if (annotationView) {
            annotationView.annotation = annotation;
        } else {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                          reuseIdentifier:identifier];
        }

        // set your annotationView properties
        
        annotationView.image = [UIImage imageNamed:@"Your image here"];
        annotationView.canShowCallout = YES;

        // if you add QuartzCore to your project, you can set shadows for your image, too
        //
        // [annotationView.layer setShadowColor:[UIColor blackColor].CGColor];
        // [annotationView.layer setShadowOpacity:1.0f];
        // [annotationView.layer setShadowRadius:5.0f];
        // [annotationView.layer setShadowOffset:CGSizeMake(0, 0)];
        // [annotationView setBackgroundColor:[UIColor whiteColor]];
        
        return annotationView;
    }

    return nil;
}

顺便说一句,在上面的示例中,我将您的annotation班级名称更改为CustomAnnotation. annotation对一个类来说是一个可怕的名字,因为 (a) 它不遵循大写首字母的类命名约定;(b) 它与变量名 相同annotation,许多MKMapViewDelegate方法默认使用。

参考

于 2013-04-14T18:09:46.897 回答
0

您当然可以使用自定义图像MKAnnotationView,请参阅iOS MapKit 自定义图钉

于 2013-04-14T17:35:09.503 回答
0

两者都是 UIView 的子类,因此将允许自定义视图MKAnnotationPinViewMKAnnotationView

于 2013-04-14T17:36:30.950 回答