MapView mit mehreren Pins

  • MapView mit mehreren Pins

    Hallo zusammen,

    ich hab da mal ne Frage zum Thema MapView und mehreren Pins, bei der Ihr mir hoffentlich helfen könnt.

    Also, ich habe eine Reihe an Koordinaten, bzw. locations, die ich auf der Karte darstelle.
    Ich füge jedem Pin eine Annotation zu. Der jeweilige Name/Titel wird auch angezeigt. Ich füge jeder Annotation auch einen Button zu, der angezeigt wird.
    Soweit so gut.

    Wenn ich nun den Button anklicke, soll eine Beschreibung der jeweiligen location angezeigt werden.
    Mein Problem liegt nun eigentlich darin zu unterscheiden, welcher Pin/location/Item angeklickt wurde. Ich brauche die jeweilige itemId, die ich dann weitergeben kann, damit ich den richtigen Inhalt (in einer neuen View) laden/darstellen kann.
    Ich habe versucht dem Button einen Tag hinzu zu fügen, der die jeweilige itemId ist, aber bekomme immer nur 0 und habe keine Ahnung, wie ich es sonst bewerkstelligen soll

    Ich füge mal hier meinen Code bei und vielleicht kann mir ja jemand helfen und sagen, wo mein Denkfehler oder sonstiges ist.


    Klasse fuer Annotation:

    Quellcode

    1. @interface DisplayMap : NSObject <MKAnnotation> {
    2. CLLocationCoordinate2D coordinate;
    3. NSString *title;
    4. NSString *subtitle;
    5. NSString *itemId;
    6. }
    7. @property (nonatomic, assign) CLLocationCoordinate2D coordinate;
    8. @property (nonatomic, copy) NSString *title;
    9. @property (nonatomic, copy) NSString *subtitle;
    10. @property (nonatomic, copy) NSString *itemId;
    11. @end
    12. #import "DisplayMap.h"
    13. @implementation DisplayMap
    14. @synthesize coordinate,title,subtitle, itemId;
    15. @end
    Alles anzeigen





    Klasse fuer MapView

    Quellcode

    1. #import <UIKit/UIKit.h>
    2. #import <MapKit/MapKit.h>
    3. #import "Item.h"
    4. @interface MapViewController : UIViewController <MKMapViewDelegate> {
    5. MKMapView *mapView;
    6. NSArray *items;
    7. Item *item;
    8. }
    9. @property NSArray *items;
    10. @property (nonatomic) IBOutlet MKMapView *mapView;
    11. @end
    12. #import "MapViewController.h"
    13. #import "DisplayMap.h"
    14. @interface MapViewController ()
    15. @end
    16. @implementation MapViewController
    17. @synthesize mapView, items;
    18. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    19. {
    20. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    21. if (self) {
    22. // Custom initialization
    23. }
    24. return self;
    25. }
    26. - (void)viewDidLoad
    27. {
    28. [super viewDidLoad];
    29. // Do any additional setup after loading the view from its nib.
    30. [mapView setMapType:MKMapTypeStandard];
    31. [mapView setZoomEnabled:YES];
    32. [mapView setScrollEnabled:YES];
    33. [mapView setDelegate:self];
    34. CLLocationCoordinate2D location;
    35. item = [[Item alloc] init];
    36. DisplayMap *ann;
    37. for (int i = 0; i < [items count]; i++) {
    38. //items = Array aus Objekten item
    39. item = [items objectAtIndex:i];
    40. // entferne Leerzeichen, Zeilenumbruch und html tags, die vorhanden sind
    41. item.x = [self stringByStrippingHTML:item.x];
    42. item.x = [self removeWhiteSpace:item.x];
    43. [item.x stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    44. item.y = [self stringByStrippingHTML:item.y];
    45. item.y = [self removeWhiteSpace:item.y];
    46. [item.y stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    47. location.latitude = [item.x doubleValue];
    48. location.longitude = [item.y doubleValue];
    49. ann = [[DisplayMap alloc] init];
    50. ann.title = item.name;
    51. ann.itemId = item.id;
    52. ann.coordinate = location;
    53. [mapView addAnnotation:ann];
    54. }
    55. }
    56. - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
    57. {
    58. MKAnnotationView* annotationView = nil;
    59. DisplayMap *csAnnotation = (DisplayMap*)annotation;
    60. NSString* identifier = @"Pin";
    61. MKPinAnnotationView* pin = (MKPinAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    62. if(nil == pin)
    63. {
    64. pin = [[MKPinAnnotationView alloc] initWithAnnotation:csAnnotation reuseIdentifier:identifier];
    65. }
    66. pin.pinColor = MKPinAnnotationColorGreen;
    67. UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    68. pin.rightCalloutAccessoryView = button;
    69. annotationView = pin;
    70. [annotationView setEnabled:YES];
    71. [annotationView setCanShowCallout:YES];
    72. return annotationView;
    73. }
    74. - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
    75. {
    76. NSLog(@"calloutAccessoryControlTapped");
    77. DisplayMap* annotation = (DisplayMap*) annotation;
    78. NSLog(@"itemId: %@", annotation.itemId);
    79. }
    Alles anzeigen



    Falls mir jemand irgendwie helfen kann wäre ich überaus dankbar.

    MfG,
    wasa71
  • ordne doch den annotations einen tag zu und die entsprechenden button erhalten den gleichen

    die action die dann vom button aufgerufen wird übergibst du den den button und kann da dann den tag auslesen

    Quellcode

    1. -(void)action:(id)sender
    2. {
    3. [sender tag];
    4. }


    entsprechend des tag kannst du dann reagieren
    Ich weiß nicht immer wovon ich rede aber ich weiß das ich Recht habe. :saint: