通过 Objective C 中的经纬度列表确定该区域中提供的经纬度

Determine a provided lat long is in the region by list of lat long in Objective C

我有一个围绕达卡市地区的 latlong 的列表 (NSMutableArray)。现在想确定一个提供的经纬度是否在达卡市地区。

我的代码如下:

+(NSMutableArray*)getAvailableAddress {
    NSMutableArray* dhakaAvailableAreas=[[NSMutableArray alloc]init];
    NSDictionary *latLongDict = @{@"lat" : @(23.665217), @"long":@(90.456556)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.710108), @"long":@(90.400022)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.707796), @"long":@(90.366647)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.744056), @"long":@(90.345334)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.751866), @"long":@(90.333149)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.768038), @"long":@(23.768038)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.774732), @"long":@(90.336199)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.796483), @"long":@(90.338031)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.833850), @"long":@(90.339256)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.853928), @"long":@(90.342309)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.867869), @"long":@(90.351460)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.887389), @"long":@(90.358176)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.899096), @"long":@(90.380142)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.882914), @"long":@(90.394773)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.886806), @"long":@(90.417956)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.899729), @"long":@(90.440122)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.895324), @"long":@(90.456267)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.861075), @"long":@(90.475134)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.840659), @"long":@(90.473056)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.828086), @"long":@(90.486782)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.793225), @"long":@(90.480572)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.782239), @"long":@(90.470264)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.770608), @"long":@(90.482614)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.753955), @"long":@(90.488093)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.747045), @"long":@(90.486714)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.742954), @"long":@(90.494262)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.732360), @"long":@(90.493535)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.717059), @"long":@(90.500136)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.710997), @"long":@(90.508137)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.710997), @"long":@(90.508137)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.693175), @"long":@(90.480623)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.672433), @"long":@(90.468799)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.667661), @"long":@(90.458352)};
    [dhakaAvailableAreas addObject:latLongDict];

    return dhakaAvailableAreas; 

}

现在如果我提供经纬度 lat=23.665219 和 long=90400023 我如何确定经纬度在达卡市地区

提前致谢。

反向地理编码请求采用纬度和经度值并找到用户可读的地址。您需要将经纬度输入反向地理编码器以确定地址(国家/地区、城市部分)并确定它是否位于您感兴趣的城市内。

您可以使用 CLGeocoder API 来实现您想要的:https://developer.apple.com/documentation/corelocation/clgeocoder 查看此处关于类似问题的讨论:How to check if lat long is in the city limits

Google 的地理编码 API 此处:https://developers.google.com/maps/documentation/geocoding/intro

在我看来,您的任务是多边形点 (PIP) 问题 [https://en.wikipedia.org/wiki/Point_in_polygon]

要实现您想要的效果,您可以使用 "Ray casting algorithm"

我很快实现了这个算法,所以你可以从这里开始

@implementation PointInRegion

+(NSMutableArray*)getAvailableAddress {
    NSMutableArray* dhakaAvailableAreas=[[NSMutableArray alloc]init];
    NSDictionary *latLongDict = @{@"lat" : @(23.665217), @"long":@(90.456556)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.710108), @"long":@(90.400022)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.707796), @"long":@(90.366647)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.744056), @"long":@(90.345334)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.751866), @"long":@(90.333149)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.768038), @"long":@(23.768038)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.774732), @"long":@(90.336199)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.796483), @"long":@(90.338031)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.833850), @"long":@(90.339256)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.853928), @"long":@(90.342309)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.867869), @"long":@(90.351460)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.887389), @"long":@(90.358176)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.899096), @"long":@(90.380142)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.882914), @"long":@(90.394773)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.886806), @"long":@(90.417956)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.899729), @"long":@(90.440122)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.895324), @"long":@(90.456267)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.861075), @"long":@(90.475134)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.840659), @"long":@(90.473056)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.828086), @"long":@(90.486782)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.793225), @"long":@(90.480572)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.782239), @"long":@(90.470264)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.770608), @"long":@(90.482614)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.753955), @"long":@(90.488093)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.747045), @"long":@(90.486714)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.742954), @"long":@(90.494262)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.732360), @"long":@(90.493535)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.717059), @"long":@(90.500136)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.710997), @"long":@(90.508137)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.710997), @"long":@(90.508137)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.693175), @"long":@(90.480623)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.672433), @"long":@(90.468799)};
    [dhakaAvailableAreas addObject:latLongDict];
    latLongDict = @{@"lat" : @(23.667661), @"long":@(90.458352)};
    [dhakaAvailableAreas addObject:latLongDict];

    return dhakaAvailableAreas;
}

+ (BOOL)checkIfPoint:(CLLocationCoordinate2D)point inRegion:(NSArray<NSDictionary<NSString *, NSNumber *> *> *)region {
    int intersectionsCount = 0;
    for (int i = 1; i < region.count; i++) {
        double lat1 = region[i - 1][@"lat"].doubleValue;
        double long1 = region[i - 1][@"long"].doubleValue;
        double lat2 = region[i][@"lat"].doubleValue;
        double long2 = region[i][@"lat"].doubleValue;

        CLLocationCoordinate2D point1;
        point1.latitude = lat1;
        point1.longitude = long1;

        CLLocationCoordinate2D point2;
        point2.latitude = lat2;
        point2.longitude = long2;


        BOOL intersects = [PointInRegion checkIfHorizontalVectorFromPoint:point intersectsEdge:point1 edgePoint2:point2];
        if (intersects) {
            intersectionsCount++;
        }
    }

    return intersectionsCount % 2 > 0;
}

+ (BOOL)checkIfHorizontalVectorFromPoint:(CLLocationCoordinate2D)point intersectsEdge:(CLLocationCoordinate2D)edgePoint1 edgePoint2:(CLLocationCoordinate2D)edgePoint2 {
    double lat = point.latitude;
    double longitude = point.longitude;

    double lat1 = edgePoint1.latitude;
    double long1 = edgePoint1.longitude;
    double lat2 = edgePoint2.latitude;
    double long2 = edgePoint2.longitude;

    if ( lat1 - lat2 == 0 ) {
        // Horizontal line
        return NO;
    }

    double x = ((long1 - long2) * lat - (long1 * lat2 - long2 * lat1)) / (lat1 - lat2);
    if (longitude < x) {
        // Intersection point lies not on the vector
        return NO;
    }
    double diff1 = lat1 - lat;
    double diff2 = lat2 - lat;
    if (fmax(diff1, diff2) > 0 && fmin(diff1, diff2) < 0) {
        // Points lie in different sides
        return YES;
    }
    return NO;
}

@end

您可以使用这样的代码:

CLLocationCoordinate2D point;
point.latitude = 23.665219;
point.longitude = 90.400023;
[PointInRegion checkIfPoint:(CLLocationCoordinate2D)point inRegion: 
[PointInRegion getAvailableAddress]];

最后我得到了一个使用 GMSGeometryContainsLocation 的解决方案,它工作得更准确和更好。

if (GMSGeometryContainsLocation(locationPoint, polygonPath, YES)) {
    NSLog(@"locationPoint is in polygonPath.");
} else {
    NSLog(@"locationPoint is NOT in polygonPath.");
}