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

时间:2019-03-31 11:34:14

标签: objective-c nsmutablearray latitude-longitude polyline region

我在达卡市地区周围有一个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 如何确定达卡市地区的经纬度

谢谢。

3 个答案:

答案 0 :(得分:1)

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

要实现您想要的目标,可以使用“射线投射算法”

我很快实现了该算法,因此您可以从此开始

@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]];

答案 1 :(得分:0)

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

您可以使用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

答案 2 :(得分:0)

最后,我使用GMSGeometryContainsLocation获得了一种解决方案,它的工作原理更加准确和完善。

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