从 plist 自定义地图注释

发布于 2024-10-16 21:20:32 字数 2736 浏览 2 评论 0原文

我找到了一个关于如何从 plist 加载“注释数据”并将这些注释(引脚)添加到 MapView 的简短指南。

我的问题是,是否有人可以告诉我如何最轻松地自定义引脚?例如,我想在 plist 中定义颜色和图像,然后在添加引脚时进行设置。

引脚添加如下:

MyAnnotation.m:

@implementation MyAnnotation
@synthesize title, subtitle, coordinate;

-(void) dealloc {
    [super dealloc];
    self.title = nil;
    self.subtitle = nil;
}

MyAnnotation.h

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

@interface MyAnnotation : NSObject<MKAnnotation> {
    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;
}

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

运行 MapViewController.m 中 ViewDidLoad 中的 plist (数组包含字典每个引脚)并添加注释。

NSString *path = [[NSBundle mainBundle] pathForResource:@"Annotations" ofType:@"plist"];
    NSMutableArray* anns = [[NSMutableArray alloc] initWithContentsOfFile:path];

    for(int i = 0; i < [anns count]; i++) {
        float realLatitude = [[[anns objectAtIndex:i] objectForKey:@"latitude"] floatValue];
        float realLongitude = [[[anns objectAtIndex:i] objectForKey:@"longitude"] floatValue];

        MyAnnotation* myAnnotation = [[MyAnnotation alloc] init];
        CLLocationCoordinate2D theCoordinate;
        theCoordinate.latitude = realLatitude;
        theCoordinate.longitude = realLongitude;
        myAnnotation.coordinate = theCoordinate;
        myAnnotation.title = [[anns objectAtIndex:i] objectForKey:@"title"];
        myAnnotation.subtitle = [[anns objectAtIndex:i] objectForKey:@"subtitle"];
        [mapView addAnnotation:myAnnotation];
        [annotations addObject:myAnnotation];
        [myAnnotation release];
    }

编辑:

我添加了 viewForAnnotation 方法来设置颜色,但如何单独设置每个引脚的颜色?

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation {
    MKPinAnnotationView *pinView = nil;

    if(annotation != mapView.userLocation) {
        static NSString *defaultPinID = @"com.invasivecode.pin";
        pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];

        pinView.pinColor = MKPinAnnotationColorPurple; 
        pinView.canShowCallout = YES;
        pinView.animatesDrop = YES;
    } 
    else {
        [mapView.userLocation setTitle:@"I am here"];
    }

    return pinView;
}

I found a short guide on how to load "annotation data" from a plist and add those annotations (pins) to a MapView.

My question is if anyone can tell me how I would customize the pins the most easily? As an example, I would like to define a color and an image in the plist and then have it set when adding the pins.

The pins are added like this:

MyAnnotation.m:

@implementation MyAnnotation
@synthesize title, subtitle, coordinate;

-(void) dealloc {
    [super dealloc];
    self.title = nil;
    self.subtitle = nil;
}

MyAnnotation.h

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

@interface MyAnnotation : NSObject<MKAnnotation> {
    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;
}

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

Running through the plist in ViewDidLoad in MapViewController.m (array containg dictionaries for each pin) and adding annotations.

NSString *path = [[NSBundle mainBundle] pathForResource:@"Annotations" ofType:@"plist"];
    NSMutableArray* anns = [[NSMutableArray alloc] initWithContentsOfFile:path];

    for(int i = 0; i < [anns count]; i++) {
        float realLatitude = [[[anns objectAtIndex:i] objectForKey:@"latitude"] floatValue];
        float realLongitude = [[[anns objectAtIndex:i] objectForKey:@"longitude"] floatValue];

        MyAnnotation* myAnnotation = [[MyAnnotation alloc] init];
        CLLocationCoordinate2D theCoordinate;
        theCoordinate.latitude = realLatitude;
        theCoordinate.longitude = realLongitude;
        myAnnotation.coordinate = theCoordinate;
        myAnnotation.title = [[anns objectAtIndex:i] objectForKey:@"title"];
        myAnnotation.subtitle = [[anns objectAtIndex:i] objectForKey:@"subtitle"];
        [mapView addAnnotation:myAnnotation];
        [annotations addObject:myAnnotation];
        [myAnnotation release];
    }

EDIT:

I have added the viewForAnnotation method which sets the color but how can I set the color of each pin individually?

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation {
    MKPinAnnotationView *pinView = nil;

    if(annotation != mapView.userLocation) {
        static NSString *defaultPinID = @"com.invasivecode.pin";
        pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];

        pinView.pinColor = MKPinAnnotationColorPurple; 
        pinView.canShowCallout = YES;
        pinView.animatesDrop = YES;
    } 
    else {
        [mapView.userLocation setTitle:@"I am here"];
    }

    return pinView;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奢望 2024-10-23 21:20:32

解决了!可以在 viewForAnnotation 方法中这样完成:

for(MyAnnotation* a in mapView.annotations) {
            if(annotation == a && annotation != mapView.userLocation) {
                pinView.image = [UIImage imageNamed:a.annImage];
            }
        }

Solved! It can be done like this in the viewForAnnotation method:

for(MyAnnotation* a in mapView.annotations) {
            if(annotation == a && annotation != mapView.userLocation) {
                pinView.image = [UIImage imageNamed:a.annImage];
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文