What i have
I have about 150 MKAnnotationViews on a map. Every MKAnnotationView has an image that replaces the default pin.
What's happening now
When the map zooms in, the MKAnnotationViews are getting smaller and the opposite when it zooms out.
What I wish Happened
Well i want it to be the other way around. Since when the map is small I wish that the MKAnnotationViews will be smaller so the user can see all of them, and when he zooms in I wish they will be bigger.
What code I have so far
I know how to get the zoom change, and i know i can get the "pMapView.region.span.latitudeDelta" as a reference for the zoom amount. and i know i can change the annotationView.frame.
-(void)mapView:(MKMapView *)pMapView regionDidChangeAnimated:(BOOL)animated{
NSLog(@"mapView.region.span.latitudeDelta = %f",pMapView.region.span.latitudeDelta);
for (id <MKAnnotation> annotation in pMapView.annotations) {
MKAnnotationView *annotationView = [pMapView viewForAnnotation: annotation];
}
}
Can someone help me with that please? Thanks shani
An alternative method would be to re-size the annotation view image property. An example is shown in this post
@Funktional's answer in Swift 3:
Sorry about my bad english... but I just want to help someone.
Thank you so much, Funktional. Your answer is great... its working on iOS5 perfectly! But this is invalidated on iOS6, and it seems there is no solution to solve this issue for now.
But finally, I solved it in a stupid/static way and it works fine on both iOS. I am not going to explain too much. This is what I've done in my project.
The image for the mkannotation is named pin.png (the size is 20x20)
I created five more images with different size for different zoom level (18x18, 16x16, 14x14, 12x12, 10x10) and they are named pin-5.png, pin-4.png, pin-3.png, pin-2.png, pin-1.png.
I removed all the code about calculating scale in formatAnnotationView and added this line
Also, change this from
UIImage *pinImage = [UIImage imageNamed:@"YOUR_IMAGE_NAME_HERE"];
to
UIImage *pinImage = [[UIImage alloc] initWithContentsOfFile:imageName];
add this function
sorry about the bad coding and english again.
In Swift 3, this worked for me. My image was 100% on level 19, so 1/19 gives me 0.5263158 which is my linear scale:
Actually on the default MKMapView- the annotation (e.g. pin or image) and the callout (e.g. bubble) remain the same size as you zoom in or out. They do not scale. But I get your point- in relation to the map they appear to be growing as the map zooms out and shrinking as the map zooms in.
So there are two solutions to your problem and they work slightly differently:
-(void)mapView:(MKMapView *)pMapView regionDidChangeAnimated:(BOOL)animated
from the MKMapViewDelegate Protocol Reference - which you've already done.UIPinchGestureRecognizer
to the MKMapView object and then implement the action.Option #1 -
mapView:regionDidChangeAnimated:
will be called for either a scroll or a zoom event - basically any time the map region changed as the name implies. This results in a slightly less smooth resizing of icons, because the map events are fired less frequently.My preferences is for Option #2 - Attach a
UIPinchGestureRecognizer
to the MKMapView object and then implement the action. Pinch gesture events are fired rather quickly, so you get a smooth resizing of the icon. And they only fire for a recognized pinch event- so they won't fire during a scroll event.The action methods invoked must conform to one of the following signatures:
You have to be careful to not override the maps default zoom behavior. See this post: "UIMapView: UIPinchGestureRecognizer not called" for more info. Short answer is that you have to implement
shouldRecognizeSimultaneouslyWithGestureRecognizer:
and return YES.All told here is some sample code:
So at this point- you again have several options for how to resize the annotation. Both of the following code samples rely on Troy Brant's code for getting the zoom level of an MKMapView.
Here is some more sample code:
If this works, don't forget to mark it as the answer.