首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何根据字符串值显示批注的不同图像

如何根据字符串值显示批注的不同图像
EN

Stack Overflow用户
提问于 2012-05-27 08:35:55
回答 1查看 2.7K关注 0票数 0

我有一个包含多个注释的MapView。我需要根据data.plist中的字符串值将不同的图像放到注释中

我的当前代码(只用于一个类别,一个图像):

代码语言:javascript
运行
复制
NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];

    NSArray *anns = [dict objectForKey:@"Category1"];
    pinView.image = [UIImage imageNamed:[[anns objectAtIndex:0] objectForKey:@"Icon"]];

    pinView.canShowCallout=YES;

我的plist文件结构:

编辑

我的MapViewController.m

代码语言:javascript
运行
复制
- (void)viewDidLoad
{
    [super viewDidLoad];


    NSMutableArray *annotations = [[NSMutableArray alloc]init];


    NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];


    NSArray *anns = [dict objectForKey:@"Category1"];

    NSLog(@" MapView is %@",mapView);
    NSLog(@"anns is: %@", anns);



    for(int i = 0; i < [anns count]; i++) {


        NSString *coordinates = [[anns objectAtIndex:i] objectForKey:@"Coordinates"];

        double realLatitude = [[[coordinates componentsSeparatedByString:@","] objectAtIndex:1] doubleValue];
        double realLongitude = [[[coordinates componentsSeparatedByString:@","] objectAtIndex:0] doubleValue];

        MyAnnotation *myAnnotation = [[MyAnnotation alloc] init];
        CLLocationCoordinate2D theCoordinate;
        theCoordinate.latitude = realLatitude;
        theCoordinate.longitude = realLongitude;


        //calculate distance
        CLLocationCoordinate2D annocoord = myAnnotation.coordinate;
        CLLocationCoordinate2D usercoord = self.mapView.userLocation.coordinate;

        NSLog(@"ANNO  = %f, %f", annocoord.latitude, annocoord.longitude);
        NSLog(@"USER = %f, %f", usercoord.latitude, usercoord.longitude);

        CLLocation *loc = [[CLLocation alloc] initWithLatitude:myAnnotation.coordinate.latitude longitude:myAnnotation.coordinate.longitude];
        CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:self.mapView.userLocation.coordinate.latitude longitude:self.mapView.userLocation.coordinate.longitude];

        NSLog(@"LOC  = %f, %f", loc.coordinate.latitude,  loc.coordinate.longitude);
        NSLog(@"LOC2 = %f, %f", loc2.coordinate.latitude, loc2.coordinate.longitude);

        CLLocationDistance dist = [loc distanceFromLocation:loc2];

        NSLog(@"DIST: %f", dist); 

        myAnnotation.coordinate=CLLocationCoordinate2DMake(realLatitude,realLongitude);        
        myAnnotation.title = [[anns objectAtIndex:i] objectForKey:@"Name"];
        myAnnotation.subtitle = [NSString stringWithFormat:@"%.f", dist];


        [mapView addAnnotation:myAnnotation];
        [annotations addObject:myAnnotation];
    }


    NSArray *alko = [dict objectForKey:@"Category2"];

    NSLog(@" MapView is %@",mapView);
    NSLog(@"alko is: %@", alko);


    for(int i = 0; i < [alko count]; i++) {


        NSString *coordinates = [[alko objectAtIndex:i] objectForKey:@"Coordinates"];

        double realLatitude = [[[coordinates componentsSeparatedByString:@","] objectAtIndex:1] doubleValue];
        double realLongitude = [[[coordinates componentsSeparatedByString:@","] objectAtIndex:0] doubleValue];

        MyAnnotation *myAnnotation = [[MyAnnotation alloc] init];
        CLLocationCoordinate2D theCoordinate;
        theCoordinate.latitude = realLatitude;
        theCoordinate.longitude = realLongitude;

        myAnnotation.coordinate=CLLocationCoordinate2DMake(realLatitude,realLongitude);        
        myAnnotation.title = [[alko objectAtIndex:i] objectForKey:@"Name"];
        myAnnotation.subtitle = [[alko objectAtIndex:i] objectForKey:@"Address"];


        [mapView addAnnotation:myAnnotation];
        [annotations addObject:myAnnotation];
    }





    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [self.locationManager startUpdatingLocation];

}

注解

代码语言:javascript
运行
复制
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{


    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;


    static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
    MKPinAnnotationView* pinView = [[MKPinAnnotationView alloc]
                                    initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];


    NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];

    NSArray *anns = [dict objectForKey:@"Category1"];
    pinView.image = [UIImage imageNamed:[[anns objectAtIndex:0] objectForKey:@"Icon"]];

    pinView.canShowCallout=YES;


    return pinView;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-27 09:29:52

这就是你需要的吗?

代码语言:javascript
运行
复制
NSString *theCategory;

// set theCategory to what you need.
theCategory = @"Category1"

NSArray *anns = [dict objectForKey:theCategory];

编辑1

首先,最好将注释类设置为:

MyAnnotation.h

代码语言:javascript
运行
复制
#import <MapKit/MapKit.h>

@interface MyAnnotation : NSManagedObject <MKAnnotation>{

CLLocationCoordinate2D coordinate;
UIImage *imageForMyAnnotation;
}

@property (nonatomic,readwrite) CLLocationCoordinate2D coordinate;
@property (nonatomic, retain) UIImage *imageForMyAnnotation;

MyAnnotation.m

代码语言:javascript
运行
复制
#import "MyAnnotation.h"

@implementation MyAnnotation

- (CLLocationCoordinate2D)coordinate
{
//here you can set the coordinate of your annotation
return coordinate;
}

- (UIImage *)imageForMyAnnotation{
//set the image here, i think it is depend on the coordinate right? You can put some code here to chose the category that corresponding to the coordinate
return image;
}

// optional
- (NSString *)title
{
return title;
}
- (NSString *)subtitle
{
return subtitle;
}

然后,在您的mapView中,您可以使用

MapView.m

代码语言:javascript
运行
复制
[self.mapView addAnnotations:myAnnotationArray];

代码语言:javascript
运行
复制
[self.mapView addAnnotation:myAnnotation];

代码语言:javascript
运行
复制
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation{

UIImageView *annotationImageView = [[UIImageView alloc] initWithImage: myAnnotation.imageForMyAnnotatioin]; 

customPinView.leftCalloutAccessoryView = annotationImageView;

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10772701

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档