Button title doesn't show with annotation - ios

Here's my problem. I created a red pin with a button thanks to my viewForAnnotation method.But the button doesn't show. Here my code:
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
//static NSString *defaultPinID = #"identifier";
MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:#"identifier"];
if ( pinView == nil )
{
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"identifier"];
pinView.enabled = YES;
pinView.pinColor=MKPinAnnotationColorRed;
pinView.canShowCallout = YES;
UIButton *btn = [UIButton buttonWithType:UIButtonTypeInfoDark];
[btn setTitle:#"test" forState:UIControlStateNormal];
pinView.rightCalloutAccessoryView = btn;
}
else
{
pinView.annotation = annotation;
}
pinView.annotation = annotation;
return pinView;
}
Can someone help me plz?

Have you added the annotation to you map?
Have you set the MKMapView's delegate?
Does your viewForAnnotation function get called?

Related

Annotation Right Accessory Button won't change

My annotations trigger the right actions, but they aren't showing the right accessory button no matter which button I choose. Below is my code.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
static NSString *s = #"ann";
MKAnnotationView *pin = [mapView dequeueReusableAnnotationViewWithIdentifier:s];
if (annotation != self.mapView.userLocation) {
pin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:s];
pin.canShowCallout = YES;
pin.calloutOffset = CGPointMake(0, 0);
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pin.rightCalloutAccessoryView = button;
[button addTarget:self
action:#selector(getDirections:)
forControlEvents:UIControlEventTouchUpInside];
}
return pin;
}
Try this:
- (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 any custom annotations.
MKAnnotationView *pin = [mapView dequeueReusableAnnotationViewWithIdentifier:#"ann"];
if(pin){
pin.annotation = annotation;
} else {
pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"ann"];
}
pin.canShowCallout = YES;
pin.calloutOffset = CGPointMake(0, 0);
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pin.rightCalloutAccessoryView = button;
[button addTarget:self
action:#selector(getDirections:)
forControlEvents:UIControlEventTouchUpInside];
return pin;
}

How can i change mapkit annotation detail view background color?

Hi am trying to change the annotation detail view(show in map image) into a custom design like the second image.
How can i change that background image and all?
my current code is
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {
mapView.showsUserLocation = YES;
if([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString *identifier = #"myAnnotation";
MKPinAnnotationView * annotationView = (MKPinAnnotationView*)[_MAPVIEW dequeueReusableAnnotationViewWithIdentifier:identifier];
if (!annotationView)
{
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.pinColor = MKPinAnnotationColorPurple;
annotationView.canShowCallout = YES;
}else {
annotationView.annotation = annotation;
}
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[infoButton addTarget:self action:#selector(showDetailView) forControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView = infoButton;
UIImageView *imageV=[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"pin.png"]];
annotationView.leftCalloutAccessoryView=imageV;
return annotationView;
}
current annotation detail view design(when i click on pin then i can see the annotation detail view )
but i want something like this design
please help me.

MapView Custom Pin Image Issue

I have changed my MapView pin image but I am getting this problem that some of the point wont change the pin image and some of them change. Where would be the problem? I have added an example.
My Code:
- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *pinAnnotation = nil;
MKAnnotationView *pinView = nil;
if(annotation != locationMap.userLocation)
{
static NSString *defaultPinID = #"myPin";
pinAnnotation = (MKPinAnnotationView *)[locationMap dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinAnnotation == nil )
pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinAnnotation.canShowCallout = YES;
pinAnnotation.animatesDrop = YES;
pinAnnotation.enabled = YES;
//pinAnnotation.image = [UIImage imageNamed:#"pin.png"];
pinView.image = [UIImage imageNamed:#"pin.png"];
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinAnnotation.rightCalloutAccessoryView = infoButton;
}
return pinAnnotation;
return pinView;
}
Use a regular MKAnnotationView, not the MKPinAnnotationView subclass. Even though you can set the image it isn't guaranteed to stick because it can and will set the pin image back again.

callout is not showing by default in map

I am trying to show callout on a pin by without clicking on the pin.In short, I want show the callout when the pins are getting placed on the map.
This is code I am using:
-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation // Method to handle all the annotations on map.
{
if([annotation isKindOfClass: [MKUserLocation class]])
return nil;
static NSString *annotationID = #"MyAnnotation";
MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationID] ;
if (!pinView) {
pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annotationID];
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:#"map_pin.png"];
//Setting Right call button
pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
//Setting Left Call button
self.favButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.favButton.frame = CGRectMake(0, 0, 23, 23);
self.favButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
self.favButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[self.favButton setImage:[UIImage imageNamed:#"favourite.png"] forState:UIControlStateNormal];
pinView.leftCalloutAccessoryView = self.favButton;
[self.mapView selectAnnotation:annotation animated:YES];
}
return pinView;
}
Any help would be appreciable.
Try with This :
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views{
for (id<MKAnnotation> currentAnnotation in mapView.annotations) {
if ([currentAnnotation isEqual:annotationToSelect]) {
[mapView selectAnnotation:currentAnnotation animated:YES];
}
}
}
OR
EDIT :
-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation // Method to handle all the annotations on map.
{
if([annotation isKindOfClass: [MKUserLocation class]])
return nil;
static NSString *annotationID = #"MyAnnotation";
MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationID] ;
if (!pinView) {
pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annotationID];
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:#"map_pin.png"];
//Setting Right call button
pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
//Setting Left Call button
self.favButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.favButton.frame = CGRectMake(0, 0, 23, 23);
self.favButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
self.favButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[self.favButton setImage:[UIImage imageNamed:#"favourite.png"] forState:UIControlStateNormal];
pinView.leftCalloutAccessoryView = self.favButton;
**[self performSelector:#selector(selectAnnotation:) withObject:annotation afterDelay:0.5];**
}
return pinView;
}
- (void)selectAnnotation:(id < MKAnnotation >)annotation
{
[self.mapView selectAnnotation:annotation animated:NO];
}

Annotation Pin as User Location [duplicate]

This question already has answers here:
How to stop the viewForAnnotation method from overriding the default user location blue beacon in iOS
(2 answers)
Closed 9 years ago.
I Implementet this:
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *pinView = nil;
static NSString *defaultPinID = #"Place to be";
pinView = (MKPinAnnotationView *)[self.myMapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil ) pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:defaultPinID];
pinView.pinColor = MKPinAnnotationColorRed;
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;
return pinView;
}
My problem now is that the Userlocation is shown a a Pin...
How can i get back the blue dot?
Thanks
try this
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
if([annotation isKindOfClass: [MKUserLocation class]])
return nil;
MKPinAnnotationView *pinView = nil;
static NSString *defaultPinID = #"Place to be";
pinView = (MKPinAnnotationView *)[self.myMapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil ) pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:defaultPinID];
pinView.pinColor = MKPinAnnotationColorRed;
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;
return pinView;
}

Resources