调整 MKAnnotationView 图像大小 地图何时放大和缩小?
我拥有的
我在地图上有大约 150 个 MKAnnotationView。 每个 MKAnnotationView 都有一个图像来替换默认的图钉。
现在发生了什么
当地图放大时,MKAnnotationViews 会变小,缩小时则相反。
我希望发生的事情
好吧,我希望情况相反。因为当地图很小时,我希望 MKAnnotationViews 会更小,以便用户可以看到所有它们,而当他放大时,我希望它们会更大。
到目前为止我有什么代码
我知道如何获取缩放更改,并且我知道我可以获取“pMapView.region.span.latitudeDelta”作为缩放量的参考。我知道我可以更改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];
}
}
有人可以帮我吗? 谢谢 沙尼
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
实际上,在默认的 MKMapView 上,注释(例如图钉或图像)和标注(例如气泡)在放大或缩小时保持相同的大小。它们无法扩展。但我明白你的观点 - 相对于地图,它们似乎随着地图缩小而增大,随着地图放大而缩小。
因此,针对您的问题有两种解决方案,它们的工作方式略有不同:
-(void )mapView:(MKMapView *)pMapView regionDidChangeAnimated:(BOOL)animated
来自 MKMapViewDelegate 协议参考 - 您已经完成了。UIPinchGestureRecognizer
附加到 MKMapView 对象,然后实现该操作。选项 #1 -
mapView:regionDidChangeAnimated:
将被调用用于滚动或缩放事件 - 基本上任何时候地图区域发生变化(顾名思义)。这会导致图标大小调整的平滑度稍差,因为地图事件触发的频率较低。我的偏好是选项 #2 - 将
UIPinchGestureRecognizer
附加到 MKMapView 对象,然后实现该操作。捏合手势事件的触发速度相当快,因此您可以平滑地调整图标大小。而且它们仅在识别的捏合事件时触发 - 因此它们不会在滚动事件期间触发。调用的操作方法必须符合以下签名之一:
您必须小心,不要覆盖地图的默认缩放行为。请参阅这篇文章:“UIMapView:未调用 UIPinchGestureRecognizer”了解更多信息。简而言之,您必须实现shouldRecognizeSimultaneouslyWithGestureRecognizer:并返回YES。
这里讲述的是一些示例代码:
因此,此时 - 您再次有几个选项来调整注释的大小。以下两个代码示例均依赖于 Troy Brant 的用于获取 MKMapView 缩放级别的代码。
这是更多示例代码:
如果这有效,请不要忘记将其标记为答案。
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.
@Funktional 在 Swift 3 中的回答:
@Funktional's answer in Swift 3:
抱歉我的英语不好...但我只是想帮助别人。
非常感谢你,Funktional。你的回答很好...它在 iOS5 上完美运行!但这个在iOS6上失效了,目前似乎还没有办法解决这个问题。
但最后,我用一种愚蠢/静态的方式解决了这个问题,并且它在两个 iOS 上都工作得很好。我不打算解释太多。这就是我在我的项目中所做的。
mkannotation 的图像名为 pin.png(大小为 20x20)
我创建了另外 5 个不同大小的图像对于不同的缩放级别(18x18、16x16、14x14、12x12、10x10),它们被命名为 pin-5.png, pin-4.png、pin-3.png、pin-2.png、pin-1.png。
我删除了 formatAnnotationView 中有关计算比例的所有代码,并添加了这一行
此外,将其更改为
UIImage *pinImage = [UIImage imageNamed:@"YOUR_IMAGE_NAME_HERE"];
至
UIImage *pinImage = [[UIImage alloc] initWithContentsOfFile:imageName];
添加此功能
再次对错误的编码和英语感到抱歉。
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.
另一种方法是重新调整注释视图图像属性的大小。 本文中显示了一个示例
An alternative method would be to re-size the annotation view image property. An example is shown in this post
在 Swift 3 中,这对我有用。我的图像在第 19 级上为 100%,因此 1/19 给我 0.5263158,这是我的线性比例:
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:
我不会使用 GestureRecognizer,因为缩放也可能以编程方式发生,而且这也不允许注释在缩放时调整大小,仅在完成缩放时才允许。我将使用委托方法
mapViewDidChangeVisibleRegion(_ mapView: MKMapView)
如下所示:您还需要此扩展:
I wouldn't use a GestureRecognizer as zooming may also happens programmatically and also this doesn't allow the annotation to resize while zooming, only when finishing zooming. I'd use the delegate method
mapViewDidChangeVisibleRegion(_ mapView: MKMapView)
like this:You also need this extension: