Custom MKAnnotationView with frame,icon and image - iphone

I've been searching for a similar solution on how to design a custom MKAnnotationView like this one.
I'v subclassed the MKAnnotation and i was able to add an image named F.png
the F.png is a frame image as showed in the picture.
what i want is to add an inner image. (colored Blue in the picture i draw)
- (MKAnnotationView *)mapView:(MKMapView *)theMapView 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];
annotationView.canShowCallout = YES;
annotationView.image = [UIImage imageNamed:[NSString stringWithFormat:#"F.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 = NO;
return annotationView;
}
return nil;
}

here in your code
else
{
MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
annotationView.canShowCallout = YES;
//change here
annotationView.image = [UIImage imageNamed:[NSString stringWithFormat:#"F.png"]];
UIImage *frame = [UIImage imageNamed:[NSString stringWithFormat:#"F.png"];
UIImage *image = theImageInFrameInner;
UIGraphicsBeginImageContext(CGSizeMake(pin.size.width, pin.size.height));
[frame drawInRect:CGRectMake(0, 0, frame.size.width, frame.size.height)];
[image drawInRect:CGRectMake(2, 2, 60, 60)]; // the frame your inner image
//maybe you should draw the left bottom icon here,
//then set back the new image, done
annotationView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
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 = NO;
return annotationView;
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *AnnotationViewID = #"annotationViewID";
MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if (annotationView == nil)
{
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
}
UIImage *imgPinBorder = [UIImage imageNamed:#"pinBorder.png"];
UIImageView *imageViewPinBorder = [[UIImageView alloc] initWithImage:imgPinBorder];
imageViewPinBorder.center = annotationView.center;
[annotationView addSubview:imageViewPinBorder];
UIImage *img = [UIImage imageNamed:#"myIcon.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:img];
imageView.center = annotationView.center;
[annotationView addSubview:imageView];
annotationView.annotation = annotation;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
annotationView.rightCalloutAccessoryView = rightButton;
annotationView.canShowCallout = YES;
annotationView.draggable = NO;
return annotationView;
}

Related

How to add UIImage in MKMapView in iPhone?

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;
}
}

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);

Custom annotation not showing callout

I am getting weird stuff in custom MKAnnotaionView. When I click on pin it does not show its detail. Am I making any mistake?
Here is my code:
- (MKAnnotationView *) mapView:(MKMapView *)mapView1 viewForAnnotation:(id
<MKAnnotation>) annotation
{
MKPinAnnotationView *annotationView = [[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:#"redpin"];
annotationView.pinColor = MKPinAnnotationColorRed;
annotationView.animatesDrop = YES;
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
button.frame = CGRectMake(0, 0, 23, 23);
annotationView.rightCalloutAccessoryView = button;
// Image and two labels
UIView *leftCAV = [[UIView alloc] initWithFrame:CGRectMake(0,0,23,23)];
UILabel *label1 = [[UILabel alloc]init];
label1.text = [NSString stringWithFormat:#"hello"];
[leftCAV addSubview :label1];
annotationView.leftCalloutAccessoryView = leftCAV;
annotationView.canShowCallout = YES;
return annotationView;
}

iphone pinView.animatesdrop not working

Here is my code of - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation method. The method is getting called but the pinView.animatesDrop = YES and pinView.canShowCallout = YES is not working. Please help
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:#","];
NSLog(#"pin map");
if(pinView == nil)
{
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#""];
pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
UIImage *image = [UIImage imageNamed:#"ann.png"];
CGRect resizeRect;
resizeRect.size = image.size;
CGSize maxSize = CGRectInset(self.view.bounds,
[map annotationPadding],
[map annotationPadding]).size;*/
maxSize.height -= self.navigationController.navigationBar.frame.size.height + [map calloutHeight];
if (resizeRect.size.width > maxSize.width)
resizeRect.size = CGSizeMake(maxSize.width, resizeRect.size.height / resizeRect.size.width * maxSize.width);
if (resizeRect.size.height > maxSize.height)
resizeRect.size = CGSizeMake(resizeRect.size.width / resizeRect.size.height * maxSize.height, maxSize.height);
resizeRect.origin = (CGPoint){0.0f, 0.0f};
UIGraphicsBeginImageContext(resizeRect.size);
[image drawInRect:resizeRect];
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
pinView.image = resizedImage;
pinView.opaque = NO;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self
action:#selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;
if (annotation == mapView.userLocation)
{
return nil;
}
return pinView;
}
else
{
pinView.annotation = annotation;
}
return pinView;
}
please see this blog-
here
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation
{
NSLog(#"welcome into the map view annotation");
// if it's the user location, just 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] autorelease];
pinView.animatesDrop=YES;
pinView.canShowCallout=YES;
pinView.pinColor=MKPinAnnotationColorPurple;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[rightButton addTarget:self
action:#selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;
UIImageView *profileIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"profile.png"]];
pinView.leftCalloutAccessoryView = profileIconView;
[profileIconView release];
return pinView;
}
I worked out what my problem was:
I had not set the delegate for the class.mapView.delegate=self;

Why does a custom MKMapView annotation image disappear on touch?

I am annotating my map and setting an image just fine, but when I tap the annotation on the MapView, the image goes from my custom image back to the red pin. Why is this?
- (MKAnnotationView *)mapView:(MKMapView *)newMapView viewForAnnotation:(id )newAnnotation {
MKPinAnnotationView *annotation = [[MKPinAnnotationView alloc] initWithAnnotation:newAnnotation reuseIdentifier:#"currentloc"];
if (annotation == nil) {
annotation = [[MKAnnotationView alloc] initWithAnnotation:newAnnotation reuseIdentifier:#"currentloc"];
}
annotation.image = [UIImage imageNamed:#"anno.png"];
annotation.canShowCallout = YES;
annotation.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"bus_stop_30x30.png"]];
annotation.leftCalloutAccessoryView = imgView;
return annotation;
}
My code looks identical to some sample code that does not produce this problem.
Answering my own question here, just in case others have the same issue. Notice that I am using "MKPinAnnotationView" - it should be changed to "MKAnnotationView" and everything works.
Fixed code:
- (MKAnnotationView *)mapView:(MKMapView *)newMapView viewForAnnotation:(id )newAnnotation {
MKAnnotationView *annotation = [[MKAnnotationView alloc] initWithAnnotation:newAnnotation reuseIdentifier:#"currentloc"];
if (annotation == nil) {
annotation = [[MKAnnotationView alloc] initWithAnnotation:newAnnotation reuseIdentifier:#"currentloc"];
}
annotation.image = [UIImage imageNamed:#"anno.png"];
annotation.canShowCallout = YES;
annotation.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"bus_stop_30x30.png"]];
annotation.leftCalloutAccessoryView = imgView;
return annotation;
}
I think this is one of the best ways:
(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
NSLog(#"welcome into the map view annotation");
MyAnnotation* myAnnotation1=annotation;
// if it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
if([mapView isEqual:mapMainView]== NO)
{
return nil;
}
// try to dequeue an existing pin view first
// if ([annotation isKindOfClass:[myAnnotation1 class]])
// {
// try to dequeue an existing pin view first
static NSString* AnnotationIdentifier = #"AnnotationIdentifier";
MKPinAnnotationView* pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if (nil == pinView)
{
MKAnnotationView* annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:AnnotationIdentifier] autorelease];
// annotationView.canShowCallout = YES;
// pinView.animatesDrop=YES;
//pinView.pinColor=MKPinAnnotationColorPurple;
//image
UIImage* flagImage = [UIImage imageNamed:#"map_marker_over2.png"];
CGRect resizeRect;
resizeRect.size = flagImage.size;
CGSize maxSize = CGRectInset(self.view.bounds,
[MapPropertyViewController annotationPadding],
[MapPropertyViewController annotationPadding]).size;
maxSize.height -= self.navigationController.navigationBar.frame.size.height + [MapPropertyViewController calloutHeight];
if (resizeRect.size.width > maxSize.width)
resizeRect.size = CGSizeMake(maxSize.width, resizeRect.size.height / resizeRect.size.width * maxSize.width);
if (resizeRect.size.height > maxSize.height)
resizeRect.size = CGSizeMake(resizeRect.size.width / resizeRect.size.height * maxSize.height, maxSize.height);
resizeRect.origin = (CGPoint){0.0f, 0.0f};
UIGraphicsBeginImageContext(resizeRect.size);
[flagImage drawInRect:resizeRect];
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
annotationView.image = resizedImage;
annotationView.opaque = NO;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[rightButton addTarget:self
action:#selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
rightButton.tag = myAnnotation1.tagAnnotation;
annotationView.rightCalloutAccessoryView = rightButton;
profileIconView = [[UIImageView alloc]initWithFrame:CGRectMake(18.5f, 10.0f, 37.0f, 30.0f)];
annotationView.leftCalloutAccessoryView = profileIconView;
//image
img = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:[arrImages objectAtIndex:myAnnotation1.tagAnnotation]]]];
NSLog(#"Annotation Click %d",[arrImages count]);
NSLog(#"image %#",img);
profileIconView.image=img;
[profileIconView release];
[annotationView setEnabled:YES];
[annotationView setCanShowCallout:YES];
return annotationView;
}
return pinView;
// }
// return nil;
}
Try this if you want to set both Source to Destination pin images different.
MKPointAnnotation * sourceAnno= [[MKPointAnnotation alloc] init];
sourceAnno.coordinate=CLLocationCoordinate2DMake(YourSourcelatitude,YourSourcelongitude);
[sourceAnno setAccessibilityLabel:#“Source”];
sourceAnno.title = #“You”;
[mapview addAnnotation: sourceAnno];
DestinationAnno = [[MKPointAnnotation alloc] init];
DestinationAnno.coordinate = CLLocationCoordinate2DMake(YourDestinationlatitude,YourDestinationlongitude);
[DestinationAnno setAccessibilityLabel:#“Destination”];
DestinationAnno.title =#“Destination”;
[mapview addAnnotation: DestinationAnno];
In Delegate Method
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPointAnnotation *PointAnno=(MKPointAnnotation *)annotation;
NSString * kPinAnnotationIdentifier = PointAnno.accessibilityLabel;
MKAnnotationView *AnnotationView = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:kPinAnnotationIdentifier];
if ([kPinAnnotationIdentifier isEqualToString:#"Source"])
{
AnnotationView.centerOffset = CGPointMake(8, -14);
AnnotationView.calloutOffset = CGPointMake(-8, 0);
AnnotationView.image = [UIImage imageNamed:#"pin_blue.png"];
AnnotationView.canShowCallout = YES;
}
else if([kPinAnnotationIdentifier isEqualToString:#"Destination"])
{
AnnotationView.centerOffset = CGPointMake(8, -14);
AnnotationView.calloutOffset = CGPointMake(-8, 0);
AnnotationView.image = [UIImage imageNamed:#"pin_red.png"];
AnnotationView.canShowCallout = YES;
}
return AnnotationView;
}
Thank you