我有 CLLocation 对象,其中包含用户的当前位置,并且对于可以倾斜的矩形的每个角,我有 4 个纬度/经度对。现在我想检查 CLLocation 坐标是否在该矩形内。
以下是矩形的坐标
#define NorthEast_LAT 51.514894
#define NorthEast_LNG -0.135306
#define SouthEast_LAT 51.514831
#define SouthEast_LNG -0.135153
#define NorthWest_LAT 51.514719
#define NorthWest_LNG -0.135858
#define SouthWest_LAT 51.514556
#define SouthWest_LNG -0.135714
我尝试了以下代码,但我认为它仅在矩形角度为 0 度时才有效。
BOOL withinRect = [delegate.CLController latlngWithInBox:location
point1:CLLocationCoordinate2DMake(NorthEast_LAT, NorthEast_LNG)
point2:CLLocationCoordinate2DMake(SouthEast_LAT, SouthEast_LNG)
point3:CLLocationCoordinate2DMake(NorthWest_LAT, NorthWest_LNG)
point4:CLLocationCoordinate2DMake(SouthWest_LAT, SouthWest_LNG)];
- (BOOL) latlngWithInBox:(CLLocation *)position point1:(CLLocationCoordinate2D)point1 point2:(CLLocationCoordinate2D)point2 point3:(CLLocationCoordinate2D)point3 point4:(CLLocationCoordinate2D)point4 {
if (position.coordinate.latitude >= point3.latitude && position.coordinate.latitude <= point2.latitude
&& position.coordinate.longitude >= point3.longitude && position.coordinate.longitude <= point2.longitude) {
return YES;
}
return NO;
}