如何测量地图移动了多远?
在我的 mapView:regionDidChangeAnimated 方法中,我正在调用以查找地图上的位置,但我只想在地图移动了很大程度时才进行调用。
以下是场景:
- 用户移动地图或地图加载
- HTTP 调用以获取查找地点
- 将地点添加到地图。
- 问题!用户单击注释打开标题气泡,它靠近边缘,因此会移动地图。由于数据加载与地图移动事件相关,标记会消失并重新添加。
我应该如何观察跨度和中心点的变化?
@Scott感谢visibleMapRect的想法。这是我到目前为止所做的工作,它仍然需要考虑放大和缩小。
MKMapRect newRect = _mapView.visibleMapRect;
MKMapRect oldRect = currentRect;
float leftBoundry = (newRect.origin.x-(newRect.size.width/4));
float rightBoundry = (newRect.origin.x+(newRect.size.width/4));
float topBoundry = (newRect.origin.y-(newRect.size.height/4));
float bottomBoundry = (newRect.origin.y+(newRect.size.height/4));
NSLog(@"Origin x %f, y %f", oldRect.origin.x, oldRect.origin.y);
NSLog(@"Boundries left %f, top %f, right %f, bottom %f", leftBoundry, topBoundry, rightBoundry, bottomBoundry);
if (oldRect.origin.x < leftBoundry || oldRect.origin.x > rightBoundry || oldRect.origin.y < topBoundry || oldRect.origin.y > bottomBoundry) {
[self loadLocations];
currentRect = newRect;
}
In my mapView:regionDidChangeAnimated method I'm making a call to find places on the map but I only want to make the call if the map has moved a significant amount.
Here is scenario:
- User moves map or map load
- HTTP call to get find places
- Add places to the map.
- PROBLEM! User clicks on an annotation opening the title bubble and it's close to the edge so it moves the map. Since the loading of data is tied to the map move event the marker disappears and is re-added.
How should I watch both the span and the center point for change?
@Scott Thanks for the visibleMapRect idea. This is what I have working so far, it still needs to account for zooming in and out.
MKMapRect newRect = _mapView.visibleMapRect;
MKMapRect oldRect = currentRect;
float leftBoundry = (newRect.origin.x-(newRect.size.width/4));
float rightBoundry = (newRect.origin.x+(newRect.size.width/4));
float topBoundry = (newRect.origin.y-(newRect.size.height/4));
float bottomBoundry = (newRect.origin.y+(newRect.size.height/4));
NSLog(@"Origin x %f, y %f", oldRect.origin.x, oldRect.origin.y);
NSLog(@"Boundries left %f, top %f, right %f, bottom %f", leftBoundry, topBoundry, rightBoundry, bottomBoundry);
if (oldRect.origin.x < leftBoundry || oldRect.origin.x > rightBoundry || oldRect.origin.y < topBoundry || oldRect.origin.y > bottomBoundry) {
[self loadLocations];
currentRect = newRect;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
唔。听起来您正在通过删除所有注释来刷新地图,然后(重新)显示位于
visibleMapRect
内的所有注释 - 并且解决此问题可能需要更细致的方法来更新地图。一种方法可能是使用 MKMapRectIntersection 来识别“旧”和“新”visibleMapRect 之间的重叠,并(如果有的话)排除该区域中的注释免遭删除或重新添加。或者,您可以计算屏幕上滚动的 L 形区域,并仅对该区域内的数据进行 HTML 调用。
或者,您可以检查地图的“旧”中心是否仍在
visibleMapRect
内,如果是,则任意确定地图没有移动很大的量。这可能会导致屏幕上出现应该有注释但实际上却没有的区域。或者,最后,您可以只存储用户选择的注释的坐标,如果地图移动后该坐标仍然在屏幕上,则找到它并重新选择注释。
Hmm. It sounds like you're refreshing your map by removing all annotations, and then (re-)displaying all annotations that fall within the
visibleMapRect
– and that solving this problem may require a more nuanced approach to updating the map.One approach might be to use
MKMapRectIntersection
to identify the overlap between your "old" and "new"visibleMapRect
s, and (if there is one) exclude the annotations in this region from being removed or re-added. Or, you could calculate the L-shaped area that's scrolling on-screen and only make an HTML call for data within that region.Or you could just check whether the map's "old" center is still within
visibleMapRect
, and if so arbitrarily decide that the map hasn't moved a significant amount. That may leave you with areas on-screen that should have annotations but don't, though.Or, finally, you could just store the coordinate of the user-selected annotation, and if that coordinate is still on-screen after the map moves, find it and re-select the annotation.