Custom annotation image in iOS

I am trying to replace a typical "pin" iOS-mapkit with a specific image. I am new to coding, so I'm not sure why this is not working, but here is what I tried:

for method

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { 

I have, among other things, the following:

 pinView.animatesDrop=YES; pinView.canShowCallout=YES; //pinView.pinColor=MKPinAnnotationColorPurple; UIImage *pinImage = [UIImage imageNamed:@"Jeff.png"]; [pinView setImage:pinImage]; 

However, although the code compiles fine, pinView does not set pinImage. Any ideas why not?

+6
source share
5 answers

all you have to do is change the last line to:

 [pinView addSubview:pinImage]; 

full example:

 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { MKPinAnnotationView *annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"ParkingPin"]; UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pin_parking.png"]] autorelease]; annView.animatesDrop = TRUE; annView.canShowCallout = YES; annView.calloutOffset = CGPointMake(-5, 5); [annView addSubview:imageView]; return annView; } 

hope this helps!

+3
source

The image should be set to the annView button button, I use it like this:

 UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; [button setImage:[UIUtils getStyleImage:@"page_map_accessory_button.png"] forState:UIControlStateNormal]; [button setImage:[UIUtils getStyleImage:@"page_map_accessory_button.png"] forState:UIControlStateSelected]; [button setImage:[UIUtils getStyleImage:@"page_map_accessory_button.png"] forState:UIControlStateHighlighted]; pinView.rightCalloutAccessoryView = button; 
+3
source

The answer that I donโ€™t see here is that you have to remove or create MKAnnotationView in the viewForAnnotation method, and also have your class as a map view delegate so that it calls this method to get the view.

If you created annotations in other places, any custom images set using the image property will not be displayed, and you will see only the output.

+2
source

Just remove the line "animatesDrop" from your code. It will work for you.

+1
source

Just do:

 annView.image=[UIImage imageNamed:@"imagename.png"]; 
0
source

Source: https://habr.com/ru/post/889391/


All Articles