作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有数百个位置的列表,只想显示当前屏幕上这些位置的 MKPinAnnotation。屏幕从 2 英里半径内的用户当前位置开始。当然,用户可以滚动和缩放屏幕。现在,我等待 map 更新事件,然后循环遍历我的位置列表,并检查坐标,如下所示:
-(void)mapViewDidFinishLoadingMap:(MKMapView *)mapView {
CGPoint point;
CLLocationCoordinate2D coordinate;
. . .
/* in location loop */
coordinate.latitude = [nextLocation getLatitude];
coordinate.longitude = [nextLocation getLongitude];
/* Determine if point is in view. Is there a better way then this? */
point = [mapView convertCoordinate:coordinate toPointToView:nil];
if( (point.x > 0) && (point.y>0) ) {
/* Add coordinate to array that is later added to mapView */
}
所以我要求转换该点在屏幕上的坐标(除非我误解了这种方法,这是很有可能的)。如果坐标不在屏幕上,那么我永远不会将其添加到 map View 中。
所以我的问题是,这是确定位置的纬度/经度是否出现在当前 View 中并应添加到 map View 的正确方法吗?或者我应该以不同的方式来做这件事?
最佳答案
在您的代码中,您应该为 toPointToView:
选项传递一个 View 。我给了它我的mapView
。您还必须指定 x 和 y 的上限。
这是一些对我有用的代码(告诉我 map 上当前可见的注释,同时循环注释):
for (Shop *shop in self.shops) {
ShopAnnotation *ann = [ShopAnnotation annotationWithShop:shop];
[self.mapView addAnnotation:ann];
CGPoint annPoint = [self.mapView convertCoordinate:ann.coordinate
toPointToView:self.mapView];
if (annPoint.x > 0.0 && annPoint.y > 0.0 &&
annPoint.x < self.mapView.frame.size.width &&
annPoint.y < self.mapView.frame.size.height) {
NSLog(@"%@ Coordinate: %f %f", ann.title, annPoint.x, annPoint.y);
}
}
关于iphone - 如何确定坐标是否在当前可见的 map 区域内?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1197398/
我有数百个位置的列表,只想显示当前屏幕上这些位置的 MKPinAnnotation。屏幕从 2 英里半径内的用户当前位置开始。当然,用户可以滚动和缩放屏幕。现在,我等待 map 更新事件,然后循环遍历
我试过检查 CGRect: CGFloat imageX1 = imageView.frame.origin.x; CGFloat imageY1 = imageView.frame.origin
我是一名优秀的程序员,十分优秀!