在 MKMapView 上移动/更新 MKOverlay
有没有办法更新(即移动)已添加到 MKMapView
的 MKOverlay
。删除旧的并添加新的非常糟糕(慢)。
即,当覆盖层在屏幕上移动时,我想触发调用此函数的后台函数:(
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
我认为使用 MKAnnotions 会好一点,但我不能使用 MKPolyline >、MKPolygon
等,整个信息被简化为单个点)
is there a way to update (i.e. moving around) a MKOverlay
that is already added to the MKMapView
. Removing a old one and adding a new one is terrible (slow).
i.e i would like to trigger the background function that is calling this function when an overlay moves on the screen:
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
(with MKAnnotions
its a little better i think, but i cant use MKPolyline
, MKPolygon
, etc. and the whole information is reduced to a single point)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Swift 3+ 更新,这对我有用,感谢@zenchemical 的启发。
旁注,我不禁觉得这不应该是必要的,并且有一个更合适的解决方案,但是,我还没有找到它。尽管添加命令被分派到主队列,但在我移动地图之前,叠加层不会重复渲染。
Swift 3+ update, this worked for me, thanks @zenchemical for the inspiration.
Side note, I can't help feeling that this shouldn't be necessary and there is a more appropriate solution, however, I haven't found it. Even though the add command is dispatched to the main queue, the overlay reproducibly does not render until I move the map.
原始
setNeedsDisplay
存在问题,如果多边形的边界矩形发生变化,那么MKMapView将无法正确更新整个矩形,其次,MKPolygon/MKPolygonRenderer
的多边形不可更新添加/删除管道的问题是,如果更新 MapView 的请求太多,管道中的速度可能会变慢,如果我添加/删除的多边形有更新,我目前使用 30Hz 线程循环there is a problem with raw
setNeedsDisplay
if the bounding rectangle of the polygon changes then MKMapView would not update whole rectangle correctly,and secondly,MKPolygon/MKPolygonRenderer
's polygon is not updatable,and the problem with add/remove pipeline is if there is too much request to update the MapView it can get slower in the pipeline, I currently use a 30Hz thread loop if there is an update in polygon i do add/removeMKOverlayView
有以下方法强制 MapKit 重新渲染给定的 mapRect:- (void)setNeedsDisplayInMapRect:(MKMapRect)mapRect
- (void)setNeedsDisplayInMapRect:( MKMapRect)mapRect ZoomScale:(MKZoomScale)zoomScale
如果您使用计时器(或定期 HTTP 请求或某种其他方法来确定您的叠加层应更新),请在overlayView 上调用上述方法之一将导致它重新渲染地图上的该点(即再次调用
-canDrawMapRect:zoomScale:
,然后调用-drawMapRect:zoomScale:inContext:
如果前者返回 YES)。更新:
如果您不确定需要重新渲染哪个mapRect,您可能可以使用
MKMapRectWorld
常量作为mapRect - 我相信这会导致覆盖整个地图以重新加载(一旦可见)。MKOverlayView
has the following methods which force MapKit to re-render the given mapRect:- (void)setNeedsDisplayInMapRect:(MKMapRect)mapRect
- (void)setNeedsDisplayInMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale
If you’re using a timer (or periodical HTTP request or some sort of other method for determining that your overlay should be updated), calling one of the above methods on the overlayView will cause it to re-render that spot on the map (i.e.
-canDrawMapRect:zoomScale:
will be called again, and then-drawMapRect:zoomScale:inContext:
will be called if the former returns YES).Update:
If you’re not sure what mapRect you need to re-render, you might be able to use the
MKMapRectWorld
constant as the mapRect — which I believe would cause the overlay across the entire map to reload (once visible).使用
MKAnnotations
。您可以更改它们的坐标。只需禁用任何与触摸相关的东西即可。您需要自己的绘图代码来绘制注释,OpenGL 可能可以做到这一点。没有人会知道其中的区别。Use
MKAnnotations
. You can change the coordinate of them. Just disable any touch-related stuff. You would need your own drawing code to draw the annotations, OpenGL would probably do the trick. Nobody would know the difference.实际上,我必须强制覆盖视图使路径无效,在设置新路径之前清除旧路径。 [polygonView 无效路径]。告诉地图视图它需要显示刷新对我来说是不够的。
I actually had to force the overlay view to invalidate the path, to clear out the old path before setting up a new one. [polygonView invalidatePath]. Telling the map view that it needs a display refresh was insufficient for me.
这似乎迫使地图刷新叠加层。大概还有注释。
[self.mapView 设置中心坐标:self.mapView.centerCoordinate];
This seems to force the map to refresh overlays. Probably annotations as well.
[self.mapView setCenterCoordinate:self.mapView.centerCoordinate];