相关疑难解决方法(0)

如何在不使用MKMapView的情况下检查MKCoordinateRegion是否包含CLLocationCoordinate2D?

我需要检查用户位置是否属于MKCoordinateRegion.我很惊讶没有为此找到简单的功能,例如:CGRectContainsCGPoint(rect,point).

我找到了以下代码:

CLLocationCoordinate2D topLeftCoordinate = 
    CLLocationCoordinate2DMake(region.center.latitude 
                               + (region.span.latitudeDelta/2.0), 
                               region.center.longitude 
                               - (region.span.longitudeDelta/2.0));


    CLLocationCoordinate2D bottomRightCoordinate = 
    CLLocationCoordinate2DMake(region.center.latitude 
                               - (region.span.latitudeDelta/2.0), 
                               region.center.longitude 
                               + (region.span.longitudeDelta/2.0));

        if (location.latitude < topLeftCoordinate.latitude || location.latitude > bottomRightCoordinate.latitude || location.longitude < bottomRightCoordinate.longitude || location.longitude > bottomRightCoordinate.longitude) {

    // Coordinate fits into the region

    }
Run Code Online (Sandbox Code Playgroud)

但是,我不确定它是否准确,因为文档没有准确指定区域矩形的计算方式.

必须有更简单的方法来做到这一点.我是否忽略了MapKit框架文档中的某些功能?

iphone mkcoordinateregion cllocationmanager cllocation ios

19
推荐指数
5
解决办法
2万
查看次数