[iPhone] Detailview linken aan annotations

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
Hallo,

Ik zit met het volgende. Ik probeer een iphone app te maken met een store locator, alleen lopen we tegen het volgende aan.

We hebben een mapview, hier worden doormiddel van een xml bestand 4 annotations ingeladen. Het probleem komt wanneer je details van deze annotation wilt bekijken. Op dit moment wordt de hele tijd dezelfde detailview geladen, dit is ook geen probleem. Maar in die detailview moet de tekst aangepast worden zodat deze overeenkomt met de aangeklikte annotation. Op dit moment hebben we gebruik gemaakt van de code KMLViewer van apple developer library om de annotations uit een xml bestand te halen en weer te geven op de mapview.

Onze detail view ziet er als volgt uit:
detailviewcontroller.m

C:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    #import "DetailViewController.h"



@implementation DetailViewController

@synthesize address;




// Implement viewDidLoad to do additional setup after loading the view, typically from a nib
- (void)viewDidLoad
{
    TabbedCalculationAppDelegate *appDelegate = (TabbedCalculationAppDelegate *)[[UIApplication sharedApplication] delegate];
    address.text = appDelegate.addressInput1 ;

    [super viewDidLoad];
}

- (void)viewDidUnload
{
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)dealloc
{
    [super dealloc];
}



Map view
C:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#import "locator.h"
#import "DetailViewController.h"

@implementation locator

@synthesize map, detailViewController, rightButton, customPinView;


- (void)viewDidLoad
{
    [super viewDidLoad];

    // create a custom navigation bar button and set it to always says "Back"
    UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
    temporaryBarButtonItem.title = @"Back";
    self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
    [temporaryBarButtonItem release];

    // Locate the path to the route.kml file in the application's bundle
    // and parse it with the KMLParser.
    NSString *path = [[NSBundle mainBundle] pathForResource:@"branches" ofType:@"kml"];
    kml = [[KMLParser parseKMLAtPath:path] retain];

    // Add all of the MKOverlay objects parsed from the KML file to the map.
    NSArray *overlays = [kml overlays];
    [map addOverlays:overlays];

    // Add all of the MKAnnotation objects parsed from the KML file to the map.
    NSArray *annotations = [kml points];

    [map addAnnotations:annotations];

    // Walk the list of overlays and annotations and create a MKMapRect that
    // bounds all of them and store it into flyTo.
    MKMapRect flyTo = MKMapRectNull;
    for (id <MKOverlay> overlay in overlays) {
        if (MKMapRectIsNull(flyTo)) {
            flyTo = [overlay boundingMapRect];
        } else {
            flyTo = MKMapRectUnion(flyTo, [overlay boundingMapRect]);
        }
    }

    for (id <MKAnnotation> annotation in annotations) {
        MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
        MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
        if (MKMapRectIsNull(flyTo)) {
            flyTo = pointRect;
        } else {
            flyTo = MKMapRectUnion(flyTo, pointRect);
        }
    }

    // Position the map so that all overlays and annotations are visible on screen.
    MKCoordinateRegion mapRegion;
    mapRegion.center.latitude = 51.522416;
    mapRegion.center.longitude = 5.141602;
    mapRegion.span.latitudeDelta = 5;
    mapRegion.span.longitudeDelta = 5;
    [map setRegion:mapRegion animated:YES];
}


#pragma mark MKMapViewDelegate

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    return [kml viewForOverlay:overlay];
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    // if it's the user location, just return nil.
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    // handle custom annotations
    //        // try to dequeue an existing pin view first
    static NSString* BridgeAnnotationIdentifier = @"bridgeAnnotationIdentifier";
    MKPinAnnotationView* pinView = (MKPinAnnotationView *)
    [map dequeueReusableAnnotationViewWithIdentifier:BridgeAnnotationIdentifier];

    if (!pinView)
    {
        // if an existing pin view was not available, create one
        customPinView = [[[MKPinAnnotationView alloc]
                                               initWithAnnotation:annotation reuseIdentifier:BridgeAnnotationIdentifier] autorelease];
        customPinView.pinColor = MKPinAnnotationColorPurple;
        customPinView.animatesDrop = YES;
        customPinView.canShowCallout = YES;

        // add a detail disclosure button to the callout which will open a new view controller page
        //
        // note: you can assign a specific call out accessory view, or as MKMapViewDelegate you can implement:
        //  - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control;
        //
        rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        customPinView.rightCalloutAccessoryView = rightButton;

        return customPinView;
    }else{
        return pinView;}

    return nil;

}

#pragma mark -
#pragma mark MKMapViewDelegate

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    {
        if (view.annotation == mapView.userLocation)
            return;

        rightButton = (DetailViewController *)view.annotation;        
        //show detail view using buttonDetail...
    }
    // the detail view does not want a toolbar so hide it
    [self.navigationController setToolbarHidden:YES animated:YES];


    [self.navigationController pushViewController:self.detailViewController animated:YES];
}

- (void)viewDidUnload
{
    self.detailViewController = nil;

}

- (void)dealloc 
{
    [detailViewController release];
    [super dealloc];
}

@end


Op dit moment weet ik even niet meer waar ik moet beginnen. Een duw in de juiste richting zou fijn zijn!!

Alvast bedankt

Acties:
  • 0 Henk 'm!

  • MacWolf
  • Registratie: Januari 2004
  • Laatst online: 06-09-2024
Je detailView zou je een aantal properties moeten geven, namelijk de velden die je wilt wijzigen als een gebruiker deze detailView toont. Zo te zien heb je al een property genaamd "address" in je detailView.

Wanneer de gebruiker dus op een annotation klikt, moet je de juiste tekst in address zetten. Dat kan je als volgt doen:

C:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    {
        if (view.annotation == mapView.userLocation)
            return;

        rightButton = (DetailViewController *)view.annotation;  
        rightButton.address = @"Straat 1, 1234 AB Stad, Land.";
    }

    // the detail view does not want a toolbar so hide it
    [self.navigationController setToolbarHidden:YES animated:YES];
    [self.navigationController pushViewController:self.detailViewController animated:YES];
}


Dat zou goed moeten werken als ik me niet vergis. Maar, mogelijk kan er nog een probleem zijn met een view die opgeslagen is in de cache, laat eerst maar weten of dit werkt.

Toevoeging: Ik heb overigens het adres er 'hard' ingezet, waarschijnlijk heb je ergens een lijst met adressen opgeslagen, je zou het juiste adres in het 'address' veld moeten zetten.

Toevoeging 2: als je Objective-C code plaatst in het forum, dat in het aan te raden om de volgende tag te gebruiken, dat maakt de code beter leesbaar:

code:
1
2
3
[code=c]
... allemaal Objective-C code ...
[/code]

[ Voor 16% gewijzigd door MacWolf op 18-03-2011 16:17 ]

Microsoft Windows: A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
Bedankt!

Ik heb het op de volgende manier opgelost:

C:
1
appDelegate.title = view.annotation.title;


Zoals je ziet heb ik gebruik gemaakt van de annotation.title property. Deze zet ik in een global zodat deze bij de detailview eruit te halen is. Vergelijkbaar met jou antwoord dus.

Alsnog bedankt voor de moeite

Acties:
  • 0 Henk 'm!

  • MacWolf
  • Registratie: Januari 2004
  • Laatst online: 06-09-2024
Edit: laat maar ...

[ Voor 96% gewijzigd door MacWolf op 23-03-2011 14:17 ]

Microsoft Windows: A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.