How Do I Add Custom Button/MapPin to MKAnnotation? - iphone

I'm a noob to iphone development and i'm trying to add a custom button to an annotation callout. I have no problems adding a regular button to rightCalloutAccessoryView, but it's just not working for custom style. My image size is 32 x 32. I also want to add a custom map pin following this stackoverflow question here Any help is greatly appreciated.
MY CODE
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
// Define your reuse identifier.
static NSString *identifier = #"MapPoint";
if ([annotation isKindOfClass:[MapPoint class]]) {
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.animatesDrop = YES;
annotationView.image = [UIImage imageNamed:#"myimage.png"];
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
//UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setImage:[UIImage imageNamed:#"phony2.png"] forState:UIControlStateNormal];
[rightButton addTarget:self
action:#selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView = rightButton;
return annotationView;
}
return nil;
}

[rightButton addTarget:self
action:#selector(showDetails:)
Remove this line
and set frame of the button
[rightButton setFrame:CGRectMake(0,0,32,32)];
and get the tap action from the degate method
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control;
where you get the annotation view.
Hope this will help you.

Related

custom callout in Mkmapview in ios

I have to display custom view on annotation click in ios, I have to display 7 images, name and detail disclosure button in custom view. On click of detail disclosure button , i have to call new view controller. How should i do it?
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
// Define your reuse identifier.
MKPinAnnotationView *annotationView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"currentloc"];
annotationView.pinColor = MKPinAnnotationColorGreen;
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
// Adding Button on annotation callout which is your detail disclosure button//
UIButton *rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0.0f,0.0f,28.0f,22.0f)];
[rightButton setImage:[UIImage imageNamed:#"rightArrow.png"] forState:UIControlStateNormal];
[rightButton addTarget:self
action:#selector(pressDetailBtn:)
forControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView = rightButton;
annotationView.image=[UIImage imageNamed:#"orangePin.png"];
annotationView.opaque = NO;
return annotationView;
}
-(void)pressDetailBtn:(id)sender
{
YourVC code here
}

Custom annotation error

I have an custom annotation set up in my mapview using below code. Problem is that the position of the annotation image is incorrect, it put's it a bit to the right of the actual location instead of spot on like when using the regular Pins.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString* AnnotationIdentifier = #"AnnotationIdentifier";
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if(annotationView)
return annotationView;
else
{
MKAnnotationView *annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:AnnotationIdentifier] autorelease];
annotationView.canShowCallout = YES;
annotationView.image = [UIImage imageNamed:[NSString stringWithFormat:#"townhouse.png"]];
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:#selector(writeSomething:) forControlEvents:UIControlEventTouchUpInside];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
annotationView.rightCalloutAccessoryView = rightButton;
annotationView.canShowCallout = YES;
annotationView.draggable = YES;
return annotationView;
}
return nil;
}
Any help at all would be grateful!
/Marcus
Marcus,
use the centerOffset property of MKAnnotationView to move your custom annotation view.
annotationView.centerOffset = CGPointMake(xOffset, yOffest);

Annotation pin color not changing and the detail disclosure is not coming in the Mapkit

- (MKPinAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id) annotation{
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"MyPin"];
annView.enabled=YES;
[annView setCanShowCallout:NO];
annView.pinColor=MKPinAnnotationColorGreen;
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
//[rightButton addTarget:self action:#selector(annotationViewClick:) forControlEvents:UIControlEventTouchUpInside];
annView.rightCalloutAccessoryView = rightButton;
return annView;
}
I m using this code only but ts not woking.....Help me!!!
Thanks in advance......
By setting [annView setCanShowCallout:NO]; the call-out for the annotation cannot be displayed. And the detail disclosure will be visible in the callout.

MapView annotation

I have added an image on annotation view and when i am touching annotation view its image is changeing into red pin can any one tell me what's he reason for it
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView* newAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"annotation1"];
if (annotation == _mapView.userLocation)
{
newAnnotation.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
return nil;
}
//[newAnnotation setSelected:YES];
newAnnotation.image = [UIImage imageNamed:#"sample.png"];
newAnnotation.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
newAnnotation.canShowCallout = YES;
[newAnnotation retain];
return newAnnotation;
}
Make the newAnnotation a MKAnnotationView rather than a MKPinAnnotationView.

Customise googlemap viewport in iphone

m stucked in figuring out how can i set the viewport for my google map markers.
is there any way to do it in objC??
or i have to do it in my map itself??? if yes, then how???
My code for google map just shows the maps along with the markers,but when i click it,it just shows the name of the place in a small box.. :(
how to make it a proper viewport???
here's the viewport image:
1) You can subclass MKAnnotationView. I think it's the best way to solve your problem.
2) You can customize your annotation callout view, e.g:
- (MKAnnotationView *) mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString* ItemAnnotationIdentifier = #"itemAnnotationIdentifier";
MKPinAnnotationView* pinView = (MKPinAnnotationView *)
[theMapView dequeueReusableAnnotationViewWithIdentifier:ItemAnnotationIdentifier];
if (!pinView)
{
// if an existing pin view was not available, create one
MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation
reuseIdentifier:ItemAnnotationIdentifier]
autorelease];
customPinView.canShowCallout = YES;
UIImage *logo = [ImageProcessor scaleImage:[UIImage imageNamed:#"logo.png"] toSize:CGSizeMake(30, 30)];
customPinView.leftCalloutAccessoryView = [[[UIImageView alloc] initWithImage:logo] autorelease];
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
rightButton.tag = [((ItemAnnotation *)annotation).itemId intValue];
[rightButton addTarget:self
action:#selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
customPinView.rightCalloutAccessoryView = rightButton;
return customPinView;
}
else
{
pinView.annotation = annotation;
}
return pinView;
}