簡體   English   中英

如何檢查CLLocationCoordinate2D是否位於四個CLLocationCoordinate2D Square內?在Objective C中使用Google Maps

[英]How can I check if a CLLocationCoordinate2D is inside four CLLocationCoordinate2D Square? in Objective C with Google Maps

我想測試一個CLLocationCoordinate2D是否在從其他四個CLLocationCoordinate2D創建的Square中

我有這樣的結構:

typedef struct {
    CLLocationCoordinate2D southWest;
    CLLocationCoordinate2D southEast;
    CLLocationCoordinate2D northEast;
    CLLocationCoordinate2D northWest;
} Bounds;

並將CLLocationCoordinate2D坐標作為參數傳遞。 我想測試坐標是否在Bounds中。 我怎么測試呢?

- (BOOL) isCoordinate:(CLLocationCoordinate2D)coordinate insideBounds:(Bounds)bounds { ... }

您只需創建一個CLregionCLCircularRegion並檢查該坐標是否包含在該區域內......

像這樣

CLCircularRegion *myRegion = [[CLCircularRegion alloc] initWithCenter:CLLocationCoordinate2DMake(22, -111) radius:500 identifier:@"myRegion"];
if ([myRegion containsCoordinate:CLLocationCoordinate2DMake(22.45, -111.1)]) {
    //Do what you want here
}

如果你有四個點你可以制作一個CGRect並使用CGRectContainsPoint()

如果4個點不代表矩形或圓形,而是地圖上的任何4個點,則需要自己創建系統。 對於在由4個定向點表示的形狀內的點,最容易檢查中心和每個連續的邊界點對之間的叉積。 對於點在邊界內的時鍾順序,所有結果必須為正。 第二件事是你需要將坐標轉換為笛卡爾系統並定位它們......讓代碼說明一切:

- (double)crossProductZCoordinateForCenter:(CLLocationCoordinate2D)center left:(CLLocationCoordinate2D)left right:(CLLocationCoordinate2D)right {
    CLLocationCoordinate2D A = CLLocationCoordinate2DMake(left.latitude-center.latitude, left.longitude-center.longitude);
    CLLocationCoordinate2D B = CLLocationCoordinate2DMake(right.latitude-center.latitude, right.longitude-center.longitude);

    return B.latitude*A.longitude - A.latitude*B.longitude;
}

- (BOOL)isCartesianCoordinate:(CLLocationCoordinate2D)coordinate insideBounds:(Bounds)bounds {
    // Now we will break the system into 4 triangles and check their orientation by using a z component of the cross product. If one of them if negative the coordinate is not inside the region
    if([self crossProductZCoordinateForCenter:coordinate left:bounds.southWest right:bounds.northWest] < .0) return NO;
    if([self crossProductZCoordinateForCenter:coordinate left:bounds.northWest right:bounds.northEast] < .0) return NO;
    if([self crossProductZCoordinateForCenter:coordinate left:bounds.northEast right:bounds.southEast] < .0) return NO;
    if([self crossProductZCoordinateForCenter:coordinate left:bounds.southEast right:bounds.southWest] < .0) return NO;
    return YES;
}

- (BOOL)isCoordinate:(CLLocationCoordinate2D)coordinate insideBounds:(Bounds)bounds {
    // We may treat the coordinates as cartesian but east should always be larger then west and north should be larger then south
    // Those smaller must be increased by 360 degrees
    if(bounds.southEast.latitude < bounds.southWest.latitude) bounds.southEast.latitude += 360.0;
    if(bounds.northEast.latitude < bounds.northWest.latitude) bounds.northEast.latitude += 360.0;
    if(bounds.northEast.longitude < bounds.southEast.longitude) bounds.northEast.longitude += 360.0;
    if(bounds.northWest.longitude < bounds.southWest.longitude) bounds.northWest.longitude += 360.0;
    // Check if any of the combination coordinates are cartesicly inside the bounds
    // We need to increase the longitude and the latitude by 360 and check all 4 combinations
    if([self isCartesianCoordinate:coordinate insideBounds:bounds]) return YES;
    if([self isCartesianCoordinate:CLLocationCoordinate2DMake(coordinate.latitude+360.0, coordinate.longitude) insideBounds:bounds]) return YES;
    if([self isCartesianCoordinate:CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude+360.0) insideBounds:bounds]) return YES;
    if([self isCartesianCoordinate:CLLocationCoordinate2DMake(coordinate.latitude+360.0, coordinate.longitude+360.0) insideBounds:bounds]) return YES;
    return NO;
}

一些簡單的測試可能派上用場:

- (void)resampleCoordinate:(CLLocationCoordinate2D *)coordinate {
    if(coordinate->latitude < -180.0) coordinate->latitude += 360.0;
    if(coordinate->latitude > 180.0) coordinate->latitude -= 360.0;
    if(coordinate->longitude < -180.0) coordinate->longitude += 360.0;
    if(coordinate->longitude > 180.0) coordinate->longitude -= 360.0;
}

- (void)testLocationSystem {
    NSInteger numberOfTests = 0;
    NSInteger numberOfTestsCheckedOut = 0;

    for(double latitude = -180.0; latitude <= 180.0; latitude++) {
        for(double longitude = -180.0; longitude <= 180.0; longitude++) {
            Bounds bounds;
            bounds.southWest = CLLocationCoordinate2DMake(latitude-15.0, longitude-15.0);
            bounds.northEast = CLLocationCoordinate2DMake(latitude+15.0, longitude+15.0);
            bounds.northWest = CLLocationCoordinate2DMake(latitude-15.0, longitude+15.0);
            bounds.southEast = CLLocationCoordinate2DMake(latitude+15.0, longitude-15.0);
            [self resampleCoordinate:&(bounds.northWest)];
            [self resampleCoordinate:&(bounds.northEast)];
            [self resampleCoordinate:&(bounds.southEast)];
            [self resampleCoordinate:&(bounds.southWest)];

            numberOfTests++;
            BOOL success = [self isCoordinate:CLLocationCoordinate2DMake(latitude, longitude) insideBounds:bounds];
            if(success) {
                numberOfTestsCheckedOut++;
            }
            else {
                NSLog(@"Failed");
            }
        }
    }
    NSLog(@"%d/%d succeeded", (int)numberOfTestsCheckedOut, (int)numberOfTests);
}
- (void)testLocationFailSystem {
    NSInteger numberOfTests = 0;
    NSInteger numberOfTestsCheckedOut = 0;

    for(double latitude = -180.0; latitude <= 180.0; latitude++) {
        for(double longitude = -180.0; longitude <= 180.0; longitude++) {
            Bounds bounds;
            bounds.southWest = CLLocationCoordinate2DMake(latitude-15.0, longitude-15.0);
            bounds.northEast = CLLocationCoordinate2DMake(latitude+15.0, longitude+15.0);
            bounds.northWest = CLLocationCoordinate2DMake(latitude-15.0, longitude+15.0);
            bounds.southEast = CLLocationCoordinate2DMake(latitude+15.0, longitude-15.0);
            [self resampleCoordinate:&(bounds.northWest)];
            [self resampleCoordinate:&(bounds.northEast)];
            [self resampleCoordinate:&(bounds.southEast)];
            [self resampleCoordinate:&(bounds.southWest)];

            for(double angle = .0f; angle < M_PI; angle+=M_PI/10.0) {
                CLLocationCoordinate2D coordiunate = CLLocationCoordinate2DMake(latitude+cos(angle)*20.0, longitude+sin(angle)*20.0);
                [self resampleCoordinate:&coordiunate];

                numberOfTests++;

                BOOL success = [self isCoordinate:coordiunate insideBounds:bounds]; // must fail
                if(success == NO) {
                    numberOfTestsCheckedOut++;
                }
                else {
                    NSLog(@"Failed");
                }
            }

        }
    }
    NSLog(@"%d/%d succeeded", (int)numberOfTestsCheckedOut, (int)numberOfTests);
}

這些都傳遞給我。 如果您發現某種情況不起作用但應該或其他方式請與我聯系。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM