简体   繁体   中英

how to show the annotation from table view to map view?

I have one tableview with names in each row and having different images for each title. I need to pass these titles onto mapview as a annotation contains title names as title for annotation.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{

UITableViewCell *cell = nil;
static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] 
            initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier];
}

AddressInfo *addressInfo = [addressList objectAtIndex:indexPath.row];



NSString *str=[NSString stringWithFormat:@"%@",addressInfo.category];
NSString *str2=@"0";
if ([str isEqualToString:str2] ) {
    UIImage *image1=[UIImage imageNamed:@"greyDot.png"];
    cell.imageView.image=image1;
}
NSString *str3=@"10";
if ([str isEqualToString:str3] ) {
    UIImage *image2=[UIImage imageNamed:@"blackDot.png"];
    cell.imageView.image=image2;
}   
NSString *str4=@"20";
if ([str isEqualToString:str4] ) {
    UIImage *image3=[UIImage imageNamed:@"greendot.png"];
    cell.imageView.image=image3;
}   

NSString *str5=@"30";
if ([str isEqualToString:str5] ) {
    UIImage *image4=[UIImage imageNamed:@"blueDot.png"];
    cell.imageView.image=image4;
}

NSString *str6=@"1";
if ([str isEqualToString:str6] ) {
    UIImage *image5=[UIImage imageNamed:@"0Dot.png"];
    cell.imageView.image=image5;
}

cell.textLabel.text = addressInfo.name;
return cell;


}

But in my mapview I'm not getting the exact image annotation as in table view, I'm getting random images as annotation from table view, but the title name appears same as I wanted.

here is my mapview code:

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id <MKAnnotation>)annotation {
   pinView = nil;
if(annotation != mapview.userLocation) 
{

    static NSString *defaultPinID = @"defaultPinID";
    pinView = (MKAnnotationView *)[mapview dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if ( pinView == nil ) pinView = [[[MKAnnotationView alloc]
                                      initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];


    AddressInfo *address = [addressArray objectAtIndex:x];
NSString *dotstr=[NSString stringWithFormat:@"%@",address.category];
    NSString *dotstr1=@"0";
    if([dotstr isEqualToString:dotstr1])
    {
        NSLog(@"string 0 is %@",dotstr1);
        pinView.image=[UIImage imageNamed:@"greyDot.png"]; 
        NSLog(@"coming here 0");
    }
    NSString *dotstr2=@"10";
    if([dotstr isEqualToString:dotstr2])
    {
        NSLog(@"string 10 is %@",dotstr2);
        pinView.image=[UIImage imageNamed:@"blackDot.png"]; 
        NSLog(@"coming here 10");
    }
    NSString *dotstr3=@"20";
    if([dotstr isEqualToString:dotstr3])
    {
        NSLog(@"string 20 is %@",dotstr3);
        pinView.image=[UIImage imageNamed:@"greendot.png"];
        NSLog(@"coming here 20");
    }

    NSString *dotstr4=@"30";
    if([dotstr isEqualToString:dotstr4])
    {
        NSLog(@"string 30 is %@",dotstr4);
        pinView.image=[UIImage imageNamed:@"blueDot.png"];
        NSLog(@"coming here 30");
    }
    NSString *dotstr5=@"1";
    if([dotstr isEqualToString:dotstr5])
    {
        NSLog(@"string defau is %@",dotstr5);
        pinView.image=[UIImage imageNamed:@"greyDot.png"];
        NSLog(@"coming here default");
    }







    pinView.canShowCallout = YES;

    infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    infoButton.tag=k;
    [infoButton addTarget:self action:@selector(pinLabelClicked:) forControlEvents:UIControlEventTouchUpInside];
    pinView.rightCalloutAccessoryView = infoButton;
    x++;


}
else {
    [mapview.userLocation setTitle:@"I am here"];
}

return pinView;


}

in first screen first rows contains blue image on left side of each row I am trying to show the same in map view but in my map view the blue annotation shows the grey image titles. does anyone have any idea how this can be done. Any tutorials or sample code?

Check this framework https://github.com/jacobjennings/JJMapCallout .

I'm use it in one my project, and it's work great. Also check discussions here and here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM