I am adding some annotations to a MKMapView and these annotations have a UIImageView, loaded asynchronously with AFNetworking, as left callout accessory view. Here's the implementation:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
static NSString *identifier = #"MapAnnotation";
if ([annotation isKindOfClass:[MapAnnotation class]]) {
MKAnnotationView *annotationView = (MKAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.image = [UIImage imageNamed:#"map_pin"];
UIImageView *userAvatar = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[userAvatar setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"https://graph.facebook.com/%#/picture?type=small",[[(MapAnnotation *)annotation user] facebookID]]] placeholderImage:[UIImage imageNamed:#"user_placeholder"]];
userAvatar.layer.cornerRadius = 4.0;
userAvatar.layer.masksToBounds = YES;
userAvatar.layer.borderColor = [[UIColor blackColor] CGColor];
userAvatar.layer.borderWidth = 1;
annotationView.leftCalloutAccessoryView = userAvatar;
UIButton *rightCallout = [UIButton buttonWithType:UIButtonTypeInfoLight];
annotationView.rightCalloutAccessoryView = rightCallout;
}
annotationView.annotation = annotation;
return annotationView;
}
return nil;
}
The problem here is: sometimes (not all the times) when annotations for different users are loaded into the map, they show the same image for all of them.
Let's say I have to load two annotations with this information:
Annotation 1
Name (title): Joshua
Datetime (subtitle): 19/06, 11:00
Image: www.myurl.com/joshua.jpg
Annotation 2
Name (title): Mathew
Datetime (subtitle): 10/06, 20:00
Image: www.myurl.com/mathew.jpg
Sometimes they are shown with title and subtitle correctly but the image loaded is the same for all of them (joshua's image or vice versa).
Am I missing something here is this method?
The call to dequeueReusableAnnotationViewWithIdentifier: will at the first call return nil which will satisfy annotationView == nil and set up your annotation view. So all the following calls to dequeueReusableAnnotationViewWithIdentifier: will return the first created annotation view with the assigned avatar image.
So [userAvatar setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"https://graph.facebook.com/%#/picture?type=small",[[(MapAnnotation *)annotation user] facebookID]]] placeholderImage:[UIImage imageNamed:#"user_placeholder"]]; is only called once.
You need to move annotation specific changes to the annotation view outside the "if nil" setup scope.
So you should instead set the userAvatar image each time the mapview ask for a annotationView.
So something like this:
MKAnnotationView *annotationView = (MKAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.image = [UIImage imageNamed:#"map_pin"];
UIImageView *userAvatar = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
userAvatar.layer.cornerRadius = 4.0;
userAvatar.layer.masksToBounds = YES;
userAvatar.layer.borderColor = [[UIColor blackColor] CGColor];
userAvatar.layer.borderWidth = 1;
annotationView.leftCalloutAccessoryView = userAvatar;
UIButton *rightCallout = [UIButton buttonWithType:UIButtonTypeInfoLight];
annotationView.rightCalloutAccessoryView = rightCallout;
}
[((UIImageView *)annotationView.leftCalloutAccessoryView) setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"https://graph.facebook.com/%#/picture?type=small",[[(MapAnnotation *)annotation user] facebookID]]] placeholderImage:[UIImage imageNamed:#"user_placeholder"]];
This will assign the correct image each time the map view asks for an annotation view.
Cheers
Morten
Related
Hello Everyone i am new in developing app for I-Phone I have stuck in the Custom ANNotationView in mapKit. When i load map first Time it shows correctly Custom View , But after scrolling map it become the red colored PIN (default annotation in MapKit). Kindly Help me . Thank You in advance.
I am using the following code in viewForAnnotation delegate
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation: (id<MKAnnotation>)annotation
{
UIImageView *AvatarView=[[UIImageView alloc]initWithFrame:CGRectMake(0,0, 30, 30)];
AvatarView.layer.cornerRadius =AvatarView.frame.size.width / 2;
AvatarView.layer.masksToBounds = YES;
AvatarView.layer.borderColor =(__bridge CGColorRef)([UIColor colorWithRed:34.0/255.0 green:28.0/255.0 blue:36.0/255.0 alpha:1.0]) ;
AvatarView.layer.borderWidth = 1.0;
AvatarView.layer.zPosition=1;
MKPinAnnotationView* pinView =
(MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:#"CustomPinAnnotationView"];
if (!pinView)
{
// Try to dequeue an existing pin view first.
MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:#"CustomPinAnnotationView"];
// If an existing pin view was not available, create one.
//pinView.animatesDrop = YES;
annotationView.canShowCallout = YES;
if([[responseDataArray objectAtIndex:k] valueForKey:#"thumbPath"]!=nil && [[[responseDataArray objectAtIndex:k] valueForKey:#"thumbPath"] length]>0)
{
NSString *imgPath =[NSString stringWithFormat:#"%#%#",WebSiteUrl,[[responseDataArray objectAtIndex:k] valueForKey:#"thumbPath"]];
k++;
NSString* webStringURL = [imgPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL* url = [NSURL URLWithString:webStringURL];
NSString *imageName = [placeholderImageArray objectAtIndex:(arc4random() %placeholderImageArray.count)];
[AvatarView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:imageName]];
}
[annotationView addSubview:AvatarView];
return annotationView;
}
else
{
k=0;
pinView.annotation = annotation;
}
return pinView;
}
I am using the following code to add Annotation
MKPointAnnotation *myAnnotation = [[MKPointAnnotation alloc] init];
myAnnotation.coordinate = coord;
myAnnotation.title = titleStr;
myAnnotation.subtitle = #"Best bar in Town";
[myLocation addAnnotation:myAnnotation];
It never goes to else part. I don't know what is the problem. Please help me to solve out this problem. A big thanks to You.
I am adding 2 annotations in mapview with leftcalloutaccessoryview (with an image) and rightcalloutacessoryview (with detail disclosure button). The problem is that it is working fine but only for one annotation, not for both. It shows one annotation with image and detail disclosure button that can be tapped while second annotation shows only title, no image and no detail disclosure button and it can not be tapped. Here is my code.
-(void)viewDidLoad{
[super viewDidLoad];
myLocationManager=[[CLLocationManager alloc] init];
geocoder = [[CLGeocoder alloc] init];
myLocationManager.delegate=self;
myLocationManager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters;
myLocationManager.distanceFilter=10.0;
[myLocationManager startUpdatingLocation];
mapView = [[MKMapView alloc]initWithFrame:
CGRectMake(10, 255, 300, 205)];
mapView.delegate = self;
mapView.centerCoordinate = CLLocationCoordinate2DMake(37.32, -122.03);
mapView.mapType = MKMapTypeStandard;
[self startAddingAnnottaions];
[self.view addSubview:mapView];
}
//startAddingAnnotations now
-(void)startAddingAnnottaions{
//first annotation
CLLocationCoordinate2D location;
location.latitude = (double) 37.331768;
location.longitude = (double) -122.020039;
MapAnnotation *newAnnotation = [[MapAnnotation alloc]
initWithTitle:#"Apple Head quaters" subtitle:#"subtitle" andCoordinate:location];
[mapView addAnnotation:newAnnotation];
//second annotation
CLLocationCoordinate2D location2;
location2.latitude = (double) 37.35239;
location2.longitude = (double) -122.025919;
MapAnnotation *newAnnotation2 = [[MapAnnotation alloc]
initWithTitle:#"Test Annotation" subtitle:#"parse" andCoordinate:location2];
[mapView addAnnotation:newAnnotation2];
}
//didAddAnnotationViews
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views
{
MKAnnotationView *annotationView = [views objectAtIndex:0];
id <MKAnnotation> mapPoint = [annotationView annotation];
annotationView.canShowCallout = YES;
annotationView.calloutOffset = CGPointMake(0, 32);
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annotationView.rightCalloutAccessoryView = rightButton;
UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"orageSquare.png"]];
annotationView.leftCalloutAccessoryView = iconView;
MKCoordinateRegion region;
region.center = [mapPoint coordinate];
MKCoordinateSpan span;
span.latitudeDelta = 0.0144927536; // 0.0144927536 is equivalent to 1 mile
span.longitudeDelta = 0.0144927536;
region.span = span;
[mv setRegion:region animated:YES];
[mv selectAnnotation:mapPoint animated:YES];
}
//callout Accessory Control Tapped
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
id <MKAnnotation> annotation = [view annotation];
NSLog(#"Right Button Pressed");
[self performSegueWithIdentifier:#"mapToOtherInfo" sender:self];
}
What am I doing wrong? It should show multiple annotations with image and detail disclosure button. Why is it showing only one annotation with them? sometimes first one works fine, sometimes second one does but both don't work fine together.
You should set leftCalloutAccessoryView and rightCalloutAccessoryView property in viewForAnnotation method, not didAddAnnotationViews. Then you can show both annotation.
example:
- (MKAnnotationView *)mapView:(MKMapView*)mapView
viewForAnnotation:(id <MKAnnotation>)annotation
{
if (annotation == mapView.userLocation) {
return nil;
}
MKPinAnnotationView *annotationView;
NSString *identifier = #"Pin";
annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (nil == annotationView) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
}
UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"frame_blue.png"]];
annotationView.leftCalloutAccessoryView = iconView;
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
return annotaionView;
}
I've got a UITableViewController subclass that is subclassed by two other classes. In some of the cells of both tables I have an image that the user can click on to perform an action. It all works and I'm happy except that in one of the 2 subclasses the first row of that table has an issue. You can only click the image by clicking to the rightmost edge of the image. If you click in the body of the image it calls the table's didSelectRowAtIndexPath instead.
What boggles me is that it works in the other class for all rows and even in the one it doesn't work for it works in all the other rows where that image shows up. Here's the code I'm using to add the image and gesture recognizer into my cell:
UIImageView *imgparent = [[UIImageView alloc] initWithFrame:CGRectMake(offsetleftmain, 24.0, 14.0, 16.0)];
imgparent.image = [UIImage imageNamed:#"item_open.png"];
imgparent.tag = ITEMOPENTAG;
// add listener
imgparent.userInteractionEnabled = YES;
UITapGestureRecognizer *singleFingerDTapParent = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(loadChildren:)];
[imgparent addGestureRecognizer:singleFingerDTapParent];
[singleFingerDTapParent release];
[cell.contentView addSubview:imgparent];
[imgparent release];
I'm testing in the simulator by the way. I've tried replacing the image with a button but still the problem persists. Any ideas?
EDIT: Here's the beginnings of the code that is called when the image is clicked. Again, this works but in the case of that one row it only works if you click on the right edge of the image. I've attached a screenshot to illustrate. The first circle indicates where I have to tap in order to get my gesture to be called. The second shows an example in the very same table that allows you to click the entire image. I'm confounded.
- (void)loadChildren:(UIGestureRecognizer *)gestureRecognizer {
NSLog(#"loadChildren");
// get the cell
UITableViewCell *cell = (UITableViewCell *)gestureRecognizer.view.superview.superview;
UIImageView *imgvw = (UIImageView *)[cell viewWithTag:ITEMOPENTAG]; // open arrow
[imgvw setHidden:YES]; // hide open arrow
imgvw = (UIImageView *)[cell viewWithTag:ITEMCLOSETAG]; // close arrow
[imgvw setHidden:NO]; // show close arrow
// get the record for the cell
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
}
EDIT: Here's the complete tableView:cellForRowAtIndexPath:indexPath. Keep in mind that it's being called from the cellForRowAtIndexPath of 2 other subclasses of this class, each simply passing in an identifier withType:
- (UITableViewCell *)tableView:(UITableView *)tv
cellForRowAtIndexPath:(NSIndexPath *)indexPath
withType:(NSString *)s_type
{
UITableViewCell *cell = nil;
// if dealing with empty region then show no tasks cell
if(s_type == #"noTasksCell"){
cell = [tv dequeueReusableCellWithIdentifier:#"noTasksCell"];
if( cell == nil ) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"noTasksCell"] autorelease];
}
cell.textLabel.text = #"No tasks";
cell.textLabel.textColor = [UIColor lightGrayColor];
cell.textLabel.font = [UIFont systemFontOfSize:14];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
UILabel *lblMain;
NSDictionary *o_rec = [self getRecForPath:indexPath];
NSString *s_cell = #"rowCell";
BOOL b_parent = NO;
BOOL b_parentOpen = NO;
BOOL b_child = NO;
BOOL b_checked = NO;
if([self isParent:indexPath]){
b_parent = YES;
b_parentOpen = !([o_rec objectForKey:#"b_open"] == nil || [[o_rec objectForKey:#"b_open"] isEqualToNumber:[NSNumber numberWithInt:0]]);
s_cell = [s_cell stringByAppendingString:#"Parent"];
}
if([o_rec objectForKey:#"b_child"] != nil){
b_child = YES;
s_cell = [s_cell stringByAppendingString:[NSString stringWithFormat:#"Child%#",[o_rec objectForKey:#"indent"]]];
}
if([[o_rec objectForKey:#"checked"] isEqualToNumber:[NSNumber numberWithInt:1]]){
b_checked = YES;
s_cell = [s_cell stringByAppendingString:#"isComplete"];
}
// add the following to the name:
// - project id
// - width of table to the name so that rotations will change the cell dequeue names
// - priority
s_cell = [s_cell stringByAppendingFormat:
#"Proj%#Width%dP%#"
,[o_rec objectForKey:#"project_id"]
,(int)tv.bounds.size.width
,[[o_rec objectForKey:#"priority"] stringValue]
];
cell = [tv dequeueReusableCellWithIdentifier:s_cell];
if( cell == nil ) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:s_cell] autorelease];
cell.textLabel.hidden = YES; // hide the regular text label
cell.selectedBackgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
cell.selectedBackgroundView.backgroundColor = [delegate colorForHexWithAlpha:0xffcc66ff];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
int offsetleftmain = 10;
if(b_child){
offsetleftmain += ([[o_rec objectForKey:#"indent"] intValue]-1) * 18;
}
if(b_parent){
// parent arrow
UIImageView *imgparent = [[UIImageView alloc] initWithFrame:CGRectMake(offsetleftmain, 24.0, 14.0, 16.0)];
imgparent.image = [UIImage imageNamed:#"item_open.png"];
imgparent.tag = ITEMOPENTAG;
// add listener
imgparent.userInteractionEnabled = YES;
UITapGestureRecognizer *singleFingerDTapParent = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(loadChildren:)];
singleFingerDTapParent.numberOfTapsRequired = 1;
singleFingerDTapParent.numberOfTouchesRequired = 1;
[imgparent addGestureRecognizer:singleFingerDTapParent];
[singleFingerDTapParent release];
[cell.contentView addSubview:imgparent];
[imgparent release];
// close arrow
UIImageView *imgparent2 = [[UIImageView alloc] initWithFrame:CGRectMake(offsetleftmain-2, 24.0, 16.0, 14.0)];
imgparent2.image = [UIImage imageNamed:#"item_close.png"];
imgparent2.tag = ITEMCLOSETAG;
imgparent2.userInteractionEnabled = YES;
UITapGestureRecognizer *singleFingerDTapParent2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideChildren:)];
[imgparent2 addGestureRecognizer:singleFingerDTapParent2];
[singleFingerDTapParent2 release];
[cell.contentView addSubview:imgparent2];
[imgparent2 release];
}
offsetleftmain += 18;
// checkbox
UIImageView *imgchk = [[UIImageView alloc] initWithFrame:CGRectMake(offsetleftmain, 20.0, 24.0, 24.0)];
imgchk.image = [UIImage imageNamed:#"check_empty.png"];
imgchk.tag = EMPTYCHECKTAG;
// add listener
imgchk.userInteractionEnabled = YES;
UITapGestureRecognizer *singleFingerDTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(checkOnItem:)];
[imgchk addGestureRecognizer:singleFingerDTap];
[singleFingerDTap release];
[cell.contentView addSubview:imgchk];
[imgchk release];
// checked checkbox
UIImageView *imgchk2 = [[UIImageView alloc] initWithFrame:CGRectMake(offsetleftmain, 20.0, 24.0, 24.0)];
imgchk2.image = [UIImage imageNamed:#"check_checked.png"];
imgchk2.tag = CHECKTAG;
// add listener
imgchk2.userInteractionEnabled = YES;
UITapGestureRecognizer *singleFingerDTap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(checkOffItem:)];
[imgchk2 addGestureRecognizer:singleFingerDTap2];
[singleFingerDTap2 release];
[cell.contentView addSubview:imgchk2];
[imgchk2 release];
offsetleftmain += 28;
// main label
lblMain = [[UILabel alloc] initWithFrame:CGRectMake(offsetleftmain, 22.0, tv.bounds.size.width-offsetleftmain-10, 30.0)];
lblMain.tag = MAINLABELTAG;
lblMain.numberOfLines = 4;
lblMain.font = delegate.font_dflt;
// change color based on priority
if (5-[[o_rec objectForKey:#"priority"] intValue] == 1)
lblMain.textColor = [UIColor redColor];
else if (5-[[o_rec objectForKey:#"priority"] intValue] == 2)
lblMain.textColor = [delegate colorForHexWithAlpha:H_P2COLOR];
else if (5-[[o_rec objectForKey:#"priority"] intValue] == 3)
lblMain.textColor = [delegate colorForHexWithAlpha:H_P3COLOR];
[cell.contentView addSubview:lblMain];
[lblMain release];
// show action sheet for long press and hold
UILongPressGestureRecognizer *clicknHold = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(cellClicknHold:)];
[cell addGestureRecognizer:clicknHold];
[clicknHold release];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// task text
lblMain = (UILabel *)[cell viewWithTag:MAINLABELTAG];
lblMain.text = [self formatContent:[o_rec objectForKey:#"content"]];
CGRect newFrame = lblMain.frame;
newFrame.size.height = [[o_rec objectForKey:#"height"] floatValue];
//newFrame.size.height = [[o_rec objectForKey:#"height"] floatValue]+12;
lblMain.frame = newFrame;
// set checked status
[(UIImageView *)[cell viewWithTag:EMPTYCHECKTAG]
setHidden:[[o_rec objectForKey:#"checked"] boolValue]];
[(UIImageView *)[cell viewWithTag:CHECKTAG]
setHidden:![[o_rec objectForKey:#"checked"] boolValue]];
// show open arrow if dealing with parent cell
if (b_parent) {
//NSLog(#"b_parentOpen:%d",b_parentOpen);
[(UIImageView *)[cell viewWithTag:ITEMOPENTAG] setHidden:b_parentOpen];
[(UIImageView *)[cell viewWithTag:ITEMCLOSETAG] setHidden:!b_parentOpen];
}
return cell;
}
Just in case someone runs into a similar issue as rare as that may be maybe this will help avoid someone going bald pulling their hair out. So my problem wasn't with the tableView itself but a sidebar I have that is similar to the facebook sidebar that slides in and out. My sidebar has a tableview in it and that table has a header on it that for some reason when it was right beside my other table was somehow preventing clicks to the image in that first row. My solution was simply to position that sidebar view a little bit further offscreen away from the view it was beside instead of being right next to it. Problem solved. Thanks to all who tried to help me out.
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
//if it's user location, return nil
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
//try to dequeue an existing pin view first
static NSString* AnnotationIdentifier = #"AnnotationIdentifier";
MKPinAnnotationView* pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
pinView.pinColor = MKPinAnnotationColorRed;
//button on the right for popup for pins
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[rightButton addTarget:self
action:#selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;
//zoom button on the left of popup for pins
UIButton* leftButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
[leftButton setTitle:annotation.title forState:UIControlStateNormal];
[leftButton addTarget:self
action:#selector(zoomToLocation:) forControlEvents:UIControlEventTouchUpInside];
pinView.leftCalloutAccessoryView = leftButton;
return pinView;
}
//for map view annotation right button
-(void)showDetails:(id)sender{
NSLog(#"Annotation Click");
//fypAppDelegate *appDelegate = (fypAppDelegate *)[[UIApplication sharedApplication] delegate];
//Attraction *attraction = (Attraction *)[appDelegate.attractions objectAtIndex:sender];
infoViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:#"info"];
self.infoView = viewController;
[self.navigationController pushViewController:infoView animated:true];
}
//for map view annotation left button
-(void)zoomToLocation:(id)sender{
NSLog(#"Annotation Click");
}
Above is the delegate for the map annotations. I am able to show the pins and show the map annotation view but I don't know how to link the button events to the next view (infoViewController).
So as you guys can see, the right button is the one I want to use to enable user to view more information about that place while the left button, I want to allow user to zoom in into the coordinates of that pin.
The data are from the database I've created. Below is how I did it just for reference (in case you guys might need it)
-(void)putPins
{
fypAppDelegate *appDelegate = (fypAppDelegate *)[[UIApplication sharedApplication] delegate]; //get data
[appDelegate readTopAttractions];
int i = 0;
int count = appDelegate.attractions.count;
self.mapAnnotations = [[NSMutableArray alloc] initWithCapacity:appDelegate.attractions.count];
while (i < count) {
Attraction *attraction = (Attraction *)[appDelegate.attractions objectAtIndex:i];
i++;
//Set coordinates for pin
CLLocationCoordinate2D location;
location.latitude = (double)[[attraction xCoor] doubleValue];
location.longitude = (double)[[attraction yCoor] doubleValue];
MapPin *mapPin = [[MapPin alloc] init];
[mapPin setCoordinate:location];
[mapPin setName: [attraction name]];
NSString *desc = [attraction description];
int i = 0, position;
while(i < 50){
if ([desc characterAtIndex:i] == ' '){
position = i;
i++;
}
else
i++;
}
desc = [#"" stringByAppendingFormat:#"%#%#", [desc substringToIndex:position], #"..."];
[mapPin setDescription: desc];
[self.mapAnnotations addObject:mapPin];
}
[self.mapView addAnnotations:self.mapAnnotations];
}
please do tell me if you guys need more details.
Thank you! =)
In your showDetails: and zoomToLocation: methods, you can get a reference to the annotation whose callout button was tapped by doing the following:
MapPin *ann = (MapPin *)[mapView.selectedAnnotations objectAtIndex:0];
In zoomToLocation: you can then zoom in to that annotation using:
[mapView setRegion:
MKCoordinateRegionMakeWithDistance(ann.coordinate, 500, 500)
//500 meters vertical span, 500 meters horizontal span
animated:YES];
In showDetails:, you can pass ann or its properties to the detail view.
By the way, instead of calling custom methods using addTarget in viewForAnnotation, you can use the map view's calloutAccessoryControlTapped delegate method which gives more direct access to the annotation that was tapped. For example:
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control
{
MapPin *ann = (MapPin *)view.annotation;
if (control == view.rightCalloutAccessoryView)
{
NSLog(#"calloutAccessoryControlTapped: control=RIGHT");
//show detail view (or you can call your custom method here)...
}
else
if (control == view.leftCalloutAccessoryView)
{
NSLog(#"calloutAccessoryControlTapped: control=LEFT");
//zoom in (or you can call your custom method here)...
}
else
{
NSLog(#"calloutAccessoryControlTapped: unknown control");
}
}
Make sure you remove the addTarget calls from viewForAnnotation if you decide to use the calloutAccessoryControlTapped delegate method.
You want to zoom in to the particular pin? Is that right?
Therefore you can use the setRegion:animated: - method from MKMapView.
Example:
mapView = MKMapView
location = CLLLocationCoordinate2D
METERS_PER_MILE = 1609.344 (defined as a Constant)
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
MKCoordinateRegion adjustedRegion = [mapView regionThatFits:region];
[mapView setRegion:adjustedRegion animated:YES];
AppleDocs
http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html
http://developer.apple.com/library/IOs/#documentation/MapKit/Reference/MapKitDataTypesReference/Reference/reference.html
I want to have a custom Annotation view that behaves exactly as the standard one, but mine needs to have an image inside and several texts, that's why I've implemented the tutorial on http://developer.apple.com/library/ios/#samplecode/WeatherMap/Introduction/Intro.html
But my problem is that I want the annotation view to hide and just show a pin, same thing as the default annotation view behaves but all annotations are showing and I cant figure out a way to hide them.
Any ideas?
Thanks.
[EDIT]
My current implementation of viewForAnnotation is:
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation{
NSLog(#"Item añadido");
static NSString *AnnotationViewID = #"annotationViewID";
CustomMKAnnotationView *annotationView =
(CustomMKAnnotationView *)[mapa dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if (annotationView == nil)
{
annotationView = [[[CustomMKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
}
annotationView.annotation = annotation;
return annotationView;
}
Becuase I need the standard bubble but with an image and a couple of UILabels. But I would like to keep the standard behaviour, that is, there's a pin when the bubble is not showing, and when you tap it shows the bubble. The content of my custom bubble is implemented in "CustomMKAnnotationView".
Which is as follows:
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if (self != nil)
{
CGRect frame = self.frame;
frame.size = CGSizeMake(10.0, 10.0);
self.frame = frame;
// self. = [super pincolor];
self.backgroundColor = [UIColor clearColor];
self.centerOffset = CGPointMake(10.0, 10.0);
}
return self;
}
- (void)drawRect:(CGRect)rect{
CustomMKAnnotation *custom = (CustomMKAnnotation *)self.annotation;
if (custom != nil)
{
NSLog(#"El nombre es: %#", [custom nombre]);
UILabel *nombre = [[UILabel alloc]init];
UILabel *media = [[UILabel alloc]init];
PDColoredProgressView *barrita = [[PDColoredProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
[barrita setTintColor:[UIColor colorWithRed:0.6 green:0.83 blue:0.91 alpha:1.0f]];
nombre.textColor = [UIColor whiteColor];
media.textColor = [UIColor whiteColor];
nombre.font = [UIFont fontWithName:#"DIN-Bold" size:14];
media.font = [UIFont fontWithName:#"DIN-Medium" size:12];
CGSize size = [[custom nombre] sizeWithFont:nombre.font constrainedToSize:CGSizeMake(300, 20) lineBreakMode:nombre.lineBreakMode];
NSLog(#"el ancho es: %f y alto %f", size.width, size.height);
nombre.backgroundColor = [UIColor clearColor];
media.backgroundColor = [UIColor clearColor];
nombre.text = [custom nombre];
barrita.progress = [custom gente];
media.text = [NSString stringWithFormat:#"Media %# años", [custom media]];
UIImageView *fondo = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"bubble_map.png"]];
nombre.frame = CGRectMake(10, 10, size.width, size.height);
media.frame = CGRectMake(10, size.height + 10, size.width, size.height);
barrita.frame = CGRectMake(10, media.frame.origin.y + 20, size.width, 10);
fondo.frame = CGRectMake(-((size.width+ 20.0f)/2), -((size.height +10)*2 + 20)-10, size.width+ 20.0f, (size.height +10)*2 + 20);
fondo.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
nombre.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
[fondo addSubview:nombre];
[fondo addSubview:media];
[fondo addSubview:barrita];
[self addSubview:fondo];
[fondo release];
[nombre release];
[media release];
}
}
If you mean hiding the details of pin, have you tried creating a custom MKPinAnnotationView and set its property of canShowCallout=NO; ?
In your mapview delegate method :
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView*pinView;
if([annotation isKindOfClass:[<yourannotationclass> class]])
{
static NSString*annoIdentifier=#"AnnotationIdentifier";
pinView=(MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:annoIdentifier];
if(pinView==nil)
{
pinView=[[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annoIdentifier]autorelease ];
}
pinView.animatesDrop=NO;
pinView.canShowCallout=NO;
pinView.pinColor=MKPinAnnotationColorRed;
}
return pinView;
}