如何确定坐标是否在当前可见的地图区域中?

rya*_*day 2 iphone cocoa-touch objective-c mapkit iphone-sdk-3.0

我有几百个位置的列表,只想为当前屏幕上的那些位置显示MKPinAnnotation.屏幕以用户当前位置开始,半径为2英里.当然,用户可以在屏幕上滚动和缩放.现在,我等待地图更新事件,然后遍历我的位置列表,并检查这样的坐标:

-(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 */
  }
Run Code Online (Sandbox Code Playgroud)

所以我问convertCoordinate哪个点在屏幕上(除非我误解了这个方法很可能).如果坐标不在屏幕上,那么我从不将它添加到mapView.

所以我的问题是,这是确定位置的纬度/经度是否会出现在当前视图中并且应该添加到mapView的正确方法吗?或者我应该以不同的方式做这件事吗?

nev*_*ing 7

在您的代码中,您应该传递该toPointToView:选项的视图.我把它给了我mapView.您还必须为x和y指定上限.

这里有一些代码对我有用(告诉我地图上当前可见的注释,同时循环注释):

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);
    }
}
Run Code Online (Sandbox Code Playgroud)


Chr*_*cci 5

我知道这是一个旧线程,不确定当时有什么可用......但你应该这样做:

// -- Your previous code and CLLocationCoordinate2D init --
MKMapRect visibleRect = [mapView visibleMapRect];
if(MKMapRectContainsPoint(visibleRect, MKMapPointForCoordinate(coordinate))) {

    // Do your stuff

}
Run Code Online (Sandbox Code Playgroud)

无需转换回屏幕空间。此外,我不确定您尝试这样做的原因,我认为当注释不在屏幕上时不添加注释很奇怪...... MapKit 已经优化了这一点并且只创建(和回收)可见的注释视图.