How to check if MKCoordinateRegion contains CLLocationCoordinate2D without using MKMapView?

后端 未结 9 817
天涯浪人
天涯浪人 2020-12-13 05:08

I need to check if user location belongs to the MKCoordinateRegion. I was surprised not to find simple function for this, something like: CGRectContainsCGPoint(rect,

9条回答
  •  情深已故
    2020-12-13 05:18

    Owen Godfrey, the objective-C code doesn´t work, this is the good code: Fails on Objective-C, this is the good code:

    /* Standardises and angle to [-180 to 180] degrees */
    - (CLLocationDegrees)standardAngle:(CLLocationDegrees)angle {
        angle=fmod(angle,360);
        return angle < -180 ? -360 - angle : angle > 180 ? 360 - 180 : angle;
    }
    
    -(BOOL)thisRegion:(MKCoordinateRegion)region containsLocation:(CLLocation *)location{
        CLLocationDegrees deltaLat =fabs([self standardAngle:(region.center.latitude-location.coordinate.latitude)]);
        CLLocationDegrees deltaLong =fabs([self standardAngle:(region.center.longitude-location.coordinate.longitude)]);
        return region.span.latitudeDelta >= deltaLat && region.span.longitudeDelta >=deltaLong;
    }
        CLLocationDegrees deltalong = fabs(standardAngle(region.center.longitude - location.coordinate.longitude));
        return region.span.latitudeDelta >= deltaLat && region.span.longitudeDelta >= deltalong;
    }
    

    Thanks!

提交回复
热议问题