04-20-2010, 06:54 AM
#1 (permalink )
Registered Member
Join Date: Mar 2010
Posts: 9
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?
07-07-2010, 07:12 AM
#2 (permalink )
Registered Member
Join Date: Dec 2008
Posts: 14
Quote:
Originally Posted by
i.konov
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
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» 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