Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-20-2010, 06:54 AM   #1 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 9
i.konov is on a distinguished road
Default viewForAnnotation not working as implemented

hello,

I'm developing a view controller for a map. I want to show just one location and drop pin in the center of the map where the location is. But somehow the viewForAnnotation method does not work. Here is my code:

Code:
#import "VenueMap.h"
#import "VenueAnnotation.h"


@implementation VenueMap

@synthesize mapView;
@synthesize mapVenue;

#pragma mark -

- (void)showVenueLocation {
	CLLocation *loc = [[CLLocation alloc] initWithLatitude:[mapVenue.latitude doubleValue] longitude:[mapVenue.longitude doubleValue]];
	
	MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(loc.coordinate, 300, 300);
	MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];
	[mapView setRegion:adjustedRegion animated: YES];
	
	VenueAnnotation *an = [[VenueAnnotation alloc] init];
	an.venueName = mapVenue.name;
	an.venueAddress = mapVenue.address;
	an.venueType = mapVenue.venuetype;
	an.coordinate = loc.coordinate;

	[mapView addAnnotation:an];

	[an release];
	[loc release];
}

- (IBAction)showUserLocation {
	NSLog(@"Entering %s", __FUNCTION__);
}

- (void)openCallout:(id <MKAnnotation>)annotation {
	[mapView selectAnnotation:annotation animated:YES];
}

#pragma mark -

- (void)viewDidLoad {
	mapView.mapType = MKMapTypeStandard;
	self.title = mapVenue.name;
	
	UIBarButtonItem *userLocationButton =	[[UIBarButtonItem alloc]
									 initWithTitle:@"Loc"
									 style:UIBarButtonItemStylePlain
									 target:self
									 action:@selector(showUserLocation:)];
	self.navigationItem.rightBarButtonItem = userLocationButton;
	[userLocationButton release];
	
	[super viewDidLoad];
}

- (void)viewDidAppear:(BOOL)animated {
	[self showVenueLocation];
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	self.mapView = nil;
	self.mapVenue = nil;
}


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

#pragma mark -
#pragma mark Map View Delegate Methods

- (MKAnnotationView *) mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>) annotation {
    static NSString *placemarkIdentifier = @"Map Location Identifier";
    if ([annotation isKindOfClass:[VenueAnnotation class]]) {
        MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[theMapView dequeueReusableAnnotationViewWithIdentifier:placemarkIdentifier];
        if (annotationView == nil)  {
            annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:placemarkIdentifier];
        }            
        else 
            annotationView.annotation = annotation;
        
        annotationView.enabled = YES;
        annotationView.animatesDrop = YES;
        annotationView.pinColor = MKPinAnnotationColorPurple;
        annotationView.canShowCallout = YES;
        [self performSelector:@selector(openCallout:) withObject:annotation afterDelay:0.5];
        
		return annotationView;
    }
    return nil;
}

- (void)mapViewDidFailLoadingMap:(MKMapView *)theMapView withError:(NSError *)error {
    UIAlertView *alert = [[UIAlertView alloc] 
                          initWithTitle:NSLocalizedString(@"Error loading map", @"Error loading map")
                          message:[error localizedDescription] 
                          delegate:nil 
                          cancelButtonTitle:NSLocalizedString(@"Okay", @"Okay") 
                          otherButtonTitles:nil];
    [alert show];
    [alert release];
}

@end

it shows the annotation but just with the regular red pin, not animating the drop and not opening the callout. any suggestions why?
i.konov is offline   Reply With Quote
Old 07-07-2010, 07:12 AM   #2 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 14
tigermain is on a distinguished road
Default

Quote:
Originally Posted by i.konov View Post
hello,

I'm developing a view controller for a map. I want to show just one location and drop pin in the center of the map where the location is. But somehow the viewForAnnotation method does not work. Here is my code:

Code:
#import "VenueMap.h"
#import "VenueAnnotation.h"


@implementation VenueMap

@synthesize mapView;
@synthesize mapVenue;

#pragma mark -

- (void)showVenueLocation {
	CLLocation *loc = [[CLLocation alloc] initWithLatitude:[mapVenue.latitude doubleValue] longitude:[mapVenue.longitude doubleValue]];
	
	MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(loc.coordinate, 300, 300);
	MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];
	[mapView setRegion:adjustedRegion animated: YES];
	
	VenueAnnotation *an = [[VenueAnnotation alloc] init];
	an.venueName = mapVenue.name;
	an.venueAddress = mapVenue.address;
	an.venueType = mapVenue.venuetype;
	an.coordinate = loc.coordinate;

	[mapView addAnnotation:an];

	[an release];
	[loc release];
}

- (IBAction)showUserLocation {
	NSLog(@"Entering %s", __FUNCTION__);
}

- (void)openCallout:(id <MKAnnotation>)annotation {
	[mapView selectAnnotation:annotation animated:YES];
}

#pragma mark -

- (void)viewDidLoad {
	mapView.mapType = MKMapTypeStandard;
	self.title = mapVenue.name;
	
	UIBarButtonItem *userLocationButton =	[[UIBarButtonItem alloc]
									 initWithTitle:@"Loc"
									 style:UIBarButtonItemStylePlain
									 target:self
									 action:@selector(showUserLocation:)];
	self.navigationItem.rightBarButtonItem = userLocationButton;
	[userLocationButton release];
	
	[super viewDidLoad];
}

- (void)viewDidAppear:(BOOL)animated {
	[self showVenueLocation];
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	self.mapView = nil;
	self.mapVenue = nil;
}


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

#pragma mark -
#pragma mark Map View Delegate Methods

- (MKAnnotationView *) mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>) annotation {
    static NSString *placemarkIdentifier = @"Map Location Identifier";
    if ([annotation isKindOfClass:[VenueAnnotation class]]) {
        MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[theMapView dequeueReusableAnnotationViewWithIdentifier:placemarkIdentifier];
        if (annotationView == nil)  {
            annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:placemarkIdentifier];
        }            
        else 
            annotationView.annotation = annotation;
        
        annotationView.enabled = YES;
        annotationView.animatesDrop = YES;
        annotationView.pinColor = MKPinAnnotationColorPurple;
        annotationView.canShowCallout = YES;
        [self performSelector:@selector(openCallout:) withObject:annotation afterDelay:0.5];
        
		return annotationView;
    }
    return nil;
}

- (void)mapViewDidFailLoadingMap:(MKMapView *)theMapView withError:(NSError *)error {
    UIAlertView *alert = [[UIAlertView alloc] 
                          initWithTitle:NSLocalizedString(@"Error loading map", @"Error loading map")
                          message:[error localizedDescription] 
                          delegate:nil 
                          cancelButtonTitle:NSLocalizedString(@"Okay", @"Okay") 
                          otherButtonTitles:nil];
    [alert show];
    [alert release];
}

@end

it shows the annotation but just with the regular red pin, not animating the drop and not opening the callout. any suggestions why?

I assume you'[ve probably fixed this, but I would suggest that you havent set the delegate of the mapview to self
tigermain is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 317
22 members and 295 guests
baja_yu, cgokey, chemistry, Domele, Duncan C, Fstuff, gbenna, givensur, guusleijsten, heshiming, HowEver, iAppDeveloper, iphonedevshani, jbro, JoeRCruso, mdpauley, n00b, newDev, seokwon lee, SLIC, stanny, WheyLabs
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,648
Threads: 94,112
Posts: 402,875
Top Poster: BrianSlick (7,990)
Welcome to our newest member, brandon6031
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:32 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0