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.
Related
I was wondering if there is any way to add a UITextField & a UIButton in MKPinAnnotationView callout bubble and in turn if the user clicked button it will call some method?
you can add your custom view instead of MKAnnotationView with bellow code..
you can simple add any control with addSubview property...
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *customPinView = nil;
if(annotation != mapView.userLocation)
{
static NSString *defaultPinID = #"com.invasivecode.pin";
pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( customPinView == nil )
customPinView = [[MKAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID];
customPinView.canShowCallout = YES;
//customPinView.animatesDrop = YES;
customPinView.image = [UIImage imageNamed:#"yourImageName"];//write your imagename
// and add UIButton with bellow code
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self
action:#selector(ShowStoreDetail:)
forControlEvents:UIControlEventTouchUpInside];
customPinView.rightCalloutAccessoryView = rightButton;here
}
else {
[mapView.userLocation setTitle:#"I am here"];
}
return pinView;
}
i hope it is useful to you...
:)
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.
I want to add a UIImage in MKMapview.
i.e.: One a particular location, I want to add an image called "apple.jpg".
I don't know how to do that.
As I have already added a draggable pin as an annotation. But I don't know if I can add multiple images or not.
For add image as a Part of MKMapView see this Tutorial...
i.ndigo mkmapview
and for add Image as a Pin on MapView then use this bellow code...
MKAnnotationView *annotationView = [[[MKAnnotationView alloc] init];
annotationView.image = [UIImage imageNamed:#"apple.png"];
annotationView.annotation = annotation;
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annotationView.rightCalloutAccessoryView.tag = 101;
annotationView.canShowCallout = YES;
[yourMapView addAnnotation:annotationView];
as #Kassem Bagher mentions, you need to create custom MKAnnotationView , check whatever tutorial (example)
Your image must be in png format see the code below.
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:#"pinView"];
if (!pinView) {
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"pinView"] autorelease];
pinView.image = [UIImage imageNamed:#"apple.png"];
pinView.canShowCallout = YES;
}
}
This is working code and i already done with below code no necessory image is .png , .jpg or .gif its any formate you can use it
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *pinAnnotation = nil;
NSString *defaultPinID = #"myPin";
pinAnnotation = (MKPinAnnotationView *)[mapview dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinAnnotation == nil )
pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinAnnotation.image = [UIImage imageNamed:#"marker_postoffice.png"];
pinAnnotation.annotation = annotation;
pinAnnotation.canShowCallout = YES;
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[infoButton addTarget:self action:#selector(showDetails)forControlEvents:UIControlEventTouchUpInside];
pinAnnotation.rightCalloutAccessoryView = infoButton;
// now you can set diff image by [annotation title] you can set diff image with if else condition like below code
if([[annotation title] isEqualToString:objAppDelegate.OfficePinTitle])
{
pinAnnotation.image = [UIImage imageNamed:#"marker_postoffice.png"];
pinAnnotation.annotation = annotation;
pinAnnotation.canShowCallout = YES;
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
infoButton addTarget:self
action:#selector(showDetails)forControlEvents:UIControlEventTouchUpInside];
pinAnnotation.rightCalloutAccessoryView = infoButton;
}
else
{
pinAnnotation.image = [UIImage imageNamed:#"marker_red_postoffice.png"];
pinAnnotation.canShowCallout = YES;
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[infoButton addTarget:self action:#selector(showDetails)forControlEvents:UIControlEventTouchUpInside];
pinAnnotation.rightCalloutAccessoryView = infoButton;
}
}
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);
I am using
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
to draw my own image for a custom pin. I would like to use a different image for different pins. I was wondering how I could differentiate which pin was calling this function.
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"MyPin"];
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
if ([annView.annotation.title isEqualToString:myLocation]) {
UIImage *pinImage = [UIImage imageNamed:#"myLocationImage.png"];
[annView setImage:pinImage];
} else {
UIImage *pinImage = [UIImage imageNamed:#"resImage.png"];
[annView setImage:pinImage];
}
return annView;
}
EDIT: Revisiting this for a new project, I realized creating different pin classes was wasteful. A better implementation would be to set the pin type, then read it out of the annotation of the MKAnnotationView. Example below.
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>) annotation {
MKPinAnnotationView *pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"PinID"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[button addTarget:self action:#selector(viewStoreDetails:) forControlEvents:UIControlEventTouchDown];
pin.rightCalloutAccessoryView = button;
pin.canShowCallout = YES;
pin.calloutOffset = CGPointMake(-5, 5);
Annotation *a = (Annotation *)pin.annotation;
int i = a.locationType;
switch (i) {
case RETAIL:
pin.image = [UIImage imageNamed:#"pin_retail.png"];
break;
case OUTLET:
pin.image = [UIImage imageNamed:#"pin_outlet.png"];
break;
case COMING_SOON:
pin.image = [UIImage imageNamed:#"pin_coming_soon.png"];
break;
case MY_LOCATION:
pin.image = [UIImage imageNamed:#"pin_my_location.png"];
break;
pin.image = [UIImage imageNamed:#"pin_retail.png"];
default:
break;
}
return [pin autorelease];
}
annView.annotation.title did the trick.