【问题标题】:MapKit custom pin annotationMapKit 自定义引脚注释
【发布时间】:2013-04-06 19:02:55
【问题描述】:

我有以下文件:

annotation.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

annotation.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,但正如你所见,我也不使用任何一种,我是否可以为引脚使用自定义图像放在屏幕上。

【问题讨论】:

  • 你的问题是什么?
  • 对不起,将编辑。我需要使用自定义图像而不是通常的红色图钉

标签: ios objective-c mapkit mkpinannotationview


【解决方案1】:

您 (a) 确保将您的视图控制器定义为您的 MKMapViewdelegate; (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 类的名称更改为 CustomAnnotationannotation 是一个可怕的类名称,因为 (a) 它不遵循大写首字母的类命名约定;并且 (b) 它与变量名称 annotation 相同,许多 MKMapViewDelegate 方法将默认使用。

参考文献

【讨论】:

  • 嗨 Rob,感谢您的回答 - 我一直看到类似的东西,但我真的不知道该放在哪里!是在 annotations.m 文件中,还是在.. customAnnotation.m 文件中?
  • @JoshBoothe 你把这个方法放在你的视图控制器中。另外,不要忘记确保将您的视图控制器设置为地图视图的delegate(您可以在 IB 中执行此操作,也可以在您的 viewDidLoad 中设置一行“self.mapView.delegate = self;” 。您可能还想在视图控制器的@interface 行的末尾添加一个“&lt;MKMapViewDelegate&gt;”。请参阅该位置感知编程指南的“注释地图”部分。跨度>
  • 我想我已经设置了委托?在我的 FirstViewController.h 中,我有插座和 @property (weak, nonatomic) IBOutlet MKMapView *mapView;
  • 你的班级名称不是应该是customAnnotation,而是CustomAnnotation。类以大写字母开头,变量以小写字母开头。这是惯例(并且恰好避免了我在回答中提到的那种混淆)。
  • @JoshBoothe IBOutlet 所做的只是定义一个您希望在 Interface Builder 中连接的插座。我是说除此之外,您还需要连接MKMapViewdelegate 属性。您可以在代码中执行此操作(如上所述)或更好,在 Interface Builder 中,选择 MKMapView 转到您的网点检查器(右侧该面板的最后一个选项卡),您将看到 @ 987654344@ 在那里列出的属性,然后将链接从该属性拖到您的视图控制器。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

    • 对objective-c还是新手,我该如何实现?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-20
    • 1970-01-01
    • 1970-01-01
    • 2020-07-19
    • 1970-01-01
    相关资源
    最近更新 更多