Getting the bounds of an MKMapView

前端 未结 10 1069
既然无缘
既然无缘 2020-11-28 20:28

In order to setup a query to an external server I want to get the bounds of the current Map View in an iPhone app I\'m building. UIView should respond to bounds but it seems

10条回答
  •  时光取名叫无心
    2020-11-28 21:03

    this website solve the problem. http://www.softwarepassion.com/how-to-get-geographic-coordinates-of-the-visible-mkmapview-area-in-ios/

    MKMapRect mRect = self.mapView.visibleMapRect;
    
    -(CLLocationCoordinate2D)getNECoordinate:(MKMapRect)mRect{
        return [self getCoordinateFromMapRectanglePoint:MKMapRectGetMaxX(mRect) y:mRect.origin.y];
    }
    -(CLLocationCoordinate2D)getNWCoordinate:(MKMapRect)mRect{
        return [self getCoordinateFromMapRectanglePoint:MKMapRectGetMinX(mRect) y:mRect.origin.y];
    }
    -(CLLocationCoordinate2D)getSECoordinate:(MKMapRect)mRect{
        return [self getCoordinateFromMapRectanglePoint:MKMapRectGetMaxX(mRect) y:MKMapRectGetMaxY(mRect)];
    }
    -(CLLocationCoordinate2D)getSWCoordinate:(MKMapRect)mRect{
        return [self getCoordinateFromMapRectanglePoint:mRect.origin.x y:MKMapRectGetMaxY(mRect)];
    }
    
    -(CLLocationCoordinate2D)getCoordinateFromMapRectanglePoint:(double)x y:(double)y{
        MKMapPoint swMapPoint = MKMapPointMake(x, y);
        return MKCoordinateForMapPoint(swMapPoint);
    }
    
    -(NSArray *)getBoundingBox:(MKMapRect)mRect{
        CLLocationCoordinate2D bottomLeft = [self getSWCoordinate:mRect];
        CLLocationCoordinate2D topRight = [self getNECoordinate:mRect];
        return @[[NSNumber numberWithDouble:bottomLeft.latitude ],
                 [NSNumber numberWithDouble:bottomLeft.longitude],
                 [NSNumber numberWithDouble:topRight.latitude],
                 [NSNumber numberWithDouble:topRight.longitude]];
    }
    

提交回复
热议问题