简体   繁体   English

根据MapKit缩放级别调整自定义MKAnnotationView的大小

[英]Resizing Custom MKAnnotationView According to MapKit Zoom Level

Here is the situation: I am displaying images on the map by using custom annotations. 情况就是这样:我正在使用自定义注释在地图上显示图像。 Everything works fine but I would like to improve it visually by resizing the AnnotationView so that it shrinks as the map is zoomed out . 一切正常但我想通过调整AnnotationView大小来改进它, 以便在缩小地图时缩小它 I want to do this because, obviously, more annotations fill up the screen and need to be shrunk so that they don't overlap each other too much and the user can see more annotations. 我想这样做是因为很明显,更多的注释填满了屏幕,需要缩小,以便它们不会相互重叠太多,用户可以看到更多的注释。 (Of course, I would like to enlarge AnnotationViews when the map is zoomed in again). (当然,我想在地图再次放大时放大AnnotationViews)。

I have achieved this by removing all annotations from the map and adding them back again, whenever regionDidChangeAnimated is called. 我通过从地图中删除所有注释并在每次调用regionDidChangeAnimated时再次添加注释来实现此目的。 However, these images are loaded from the internet, so downloading the images again (since the annotations have been removed) doesn't seem to make sense. 但是,这些图像是从互联网上加载的,因此再次下载图像(因为注释已被删除)似乎没有意义。

I hope I have explained the situation well, and hope to contribute to SO in the future. 我希望我已经很好地解释了这一情况,并希望将来能够为SO作出贡献。

Thanks in advance. 提前致谢。

PS I'm developing with iOS 4.0 PS我正在开发iOS 4.0

Since you are using iOS 4.0 I would suggest using a class that conforms to the MKOverlay protocol instead of MKAnnotationView. 由于您使用的是iOS 4.0,我建议使用符合MKOverlay协议的类而不是MKAnnotationView。 Overlays zoom with the map and will scale appropriately. 叠加层随地图缩放并适当缩放。 You can probably use 1 overlay to handle all your annotations. 您可以使用1个叠加来处理所有注释。 Check out the HazardMap demo Apple provided in the 2010 WWDC talks on http://developer.apple.com 查看Apple在http://developer.apple.com上的2010 WWDC会谈中提供的HazardMap演示

Alternatively, you could use something like the k nearest neighbors algorithm to group annotations per zoom scale. 或者,您可以使用类似k近邻算法的内容来按缩放比例对注释进行分组。 However, that can be kind of slow if you have a lot of annotations. 但是,如果你有很多注释,那可能会很慢。 I tried it once with several thousand annotations and did not like the performance hit that was incurred. 我用几千个注释尝试了一次,并且不喜欢发生的性能损失。 I think < 1000 annotations might have decent performance with this method though. 我认为<1000注释可能会有这种方法的不错表现。

It sounds like you're already on the right path and that the only real problem is the downloading of the images. 听起来你已经走在了正确的道路上,唯一真正的问题是下载图像。 Is there any particular reason you can't cache them by writing them to the documents directory (or the cache directory if you want them to go way when the app quits) when you receive them and look there first before displaying them? 有没有什么特别的原因你不能通过将它们写入文档目录(或缓存目录,如果你希望它们在应用程序退出时继续)来缓存它们,当你收到它们并在显示它们之前先查看它们?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM