如何在填充mapview时设置两种不同的引脚颜色

时间:2012-02-04 08:58:17

标签: objective-c ios

我正在解析两个json提要。第一个json feed解析特定区域的药房并填充mapview,第二个feed解析该区域中存在的汉堡店列表并填充mapview。我想要的是将代表第一个馈送的引脚设置为红色,将第二个馈送设置为绿色。我是个菜鸟,你能救我​​吗?以下是代码。

- (void)viewDidLoad 
{
    [super viewDidLoad];
    self.title=@"Map";
    [self secondMap];
    [self firstMap];
}

-(void)secondMap
{
     jsonurl1=[NSURL URLWithString:@"http://dev-parkguiden.knutpunkten.se/Api/GetPharmacyJson"];
     NSURLRequest *request1 = [NSURLRequest requestWithURL:jsonurl1 cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
     connection1=[[[NSURLConnection alloc] initWithRequest:request1 delegate:self] autorelease];

     self.jsonData1=[[NSString alloc]initWithContentsOfURL:jsonurl1];

     self.jsonArray1 = [self.jsonData1 objectFromJSONString]; 
     NSLog(@"blah:%@",jsonArray1);
     items= [self.jsonArray1 objectForKey:@"pharmacies"];
     NSDictionary *bam=[items valueForKey:@"location"];

     for (NSDictionary *item in bam)
     {
         latitude=[[item valueForKey:@"latitude"] floatValue];
         longitude=[[item valueForKey:@"longitude"] floatValue];

         NSLog(@"String3:%f",latitude);
         NSLog(@"String4:%f",longitude);
         Place* home = [[[Place alloc] init] autorelease];
         home.name = [item valueForKey:@"Name"];
         // home.description=[item valueForKey:@"description"];
         home.latitude = latitude;
         home.longitude = longitude; 

         PlaceMark *from = [[[PlaceMark alloc] initWithPlace:home] autorelease];                
         [mapView addAnnotation:from];

         NSLog(@"the latitude:%f",latitude);
         NSLog(@"the latitude:%f",longitude);
         [self centerMap];
     }
}

-(void)firstMap
{
    jsonurl=[NSURL URLWithString:@"http://dev-parkguiden.knutpunkten.se/Api/WhereIam?longitude=18.105469&latitude=59.304786&distance=100"];
    NSURLRequest *request = [NSURLRequest requestWithURL:jsonurl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    connection1=[[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];

    //NSLog(@"jsonurl:%@",jsonurl);
    self.jsonData=[[NSString alloc]initWithContentsOfURL:jsonurl];

    self.jsonArray = [self.jsonData objectFromJSONString]; 
    NSLog(@"blah:%@",jsonArray);
    items = [self.jsonArray objectForKey:@"parks"];

    story = [[NSMutableArray array]retain];
    media1 = [[NSMutableArray array]retain];
    url=[[NSMutableArray array]retain];
    descriptiondesc=[[NSMutableArray array]retain];
    for (NSDictionary *item in items )
    {
        latitude=[[item valueForKey:@"latitude"] floatValue];
        longitude=[[item valueForKey:@"longitude"] floatValue];

        NSLog(@"String1:%f",latitude);
        NSLog(@"String2:%f",longitude);
        Place* home = [[[Place alloc] init] autorelease];
        home.name = [item valueForKey:@"Name"];
        //  home.description=[item valueForKey:@"description"];
        home.latitude = latitude;
        home.longitude = longitude; 

        PlaceMark *from = [[[PlaceMark alloc] initWithPlace:home] autorelease];             
        [mapView addAnnotation:from];

        NSLog(@"the latitude:%f",latitude);
        NSLog(@"the latitude:%f",longitude);
        [self centerMap];
    }
}

-(void) centerMap
{
    MKCoordinateRegion region;
    MKCoordinateSpan span;
    span.longitudeDelta=0.1;
    span.latitudeDelta=0.1;

    CLLocationCoordinate2D location=mapView.userLocation.coordinate;
    location.latitude=latitude;
    location.longitude=longitude;
    region.span=span;
    region.center=location;

    [mapView setRegion:region animated:TRUE];
    [mapView regionThatFits:region];
}

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id       <MKAnnotation>)annotation
{
    mapView.mapType=MKMapTypeHybrid;
    mapView.showsUserLocation=YES;
    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc]   initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
    annView.pinColor = MKPinAnnotationColorGreen;
    annView.animatesDrop=TRUE;
    annView.canShowCallout = YES;
    annView.calloutOffset = CGPointMake(-5, 5); 

    UIButton *detailButton=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    //NSInteger annotationValue=[self.annotations indexOfObject:annotation];

    [detailButton addTarget:self action:@selector(showDetailView)        forControlEvents:UIControlEventTouchUpInside];
    annView.rightCalloutAccessoryView=detailButton;
    return annView;
}

1 个答案:

答案 0 :(得分:2)

在我看来,您已经创建了符合PlaceMark协议的MKAnnotation类。因此,您可以做的是为其添加一个属性,以确定它是药店还是汉堡店,或者只是设置其颜色。然后,在创建它时,请确保设置该属性:

PlaceMark *from = [[[PlaceMark alloc] initWithPlace:home] autorelease];
from.colorName = @"red";
[mapView addAnnotation:from];

然后,在你的mapView:viewForAnnotation:方法中,你需要从你传递的注释中获取它。

 - (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id       <MKAnnotation>)annotation
 {
    PlaceMark *placeMark = (PlaceMark*) annotation;

    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc]   initWithAnnotation:annotation reuseIdentifier:@"currentloc"];

    if( [placeMark.colorName isEqualToString:@"red"] ) {
        annView.pinColor = MKPinAnnotationColorRed;
    } else {
        annView.pinColor = MKPinAnnotationColorGreen;
    }

    ...
 }