你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS判斷點是否在多邊形面內(高德地圖開發)

iOS判斷點是否在多邊形面內(高德地圖開發)

編輯:IOS開發綜合

定義兩個屬性

@interface OutDoorViewController ()
@property (nonatomic, strong) MAMapView *maMapView;
@property (nonatomic, strong) MAPolygonView *polygonView;
@end

初始化指定代理人
self.maMapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
    self.maMapView.delegate = self;
    [self.view addSubview:self.maMapView];

構造一個多邊形
//構造多邊形數據對象
    CLLocationCoordinate2D coordinates[4];
    coordinates[0].latitude = 39.810892;
    coordinates[0].longitude = 116.233413;
    
    coordinates[1].latitude = 39.816600;
    coordinates[1].longitude = 116.331842;
    
    coordinates[2].latitude = 39.762187;
    coordinates[2].longitude = 116.357932;
    
    coordinates[3].latitude = 39.733653;
    coordinates[3].longitude = 116.278255;
    
    MAPolygon *polygon = [MAPolygon polygonWithCoordinates:coordinates count:4];
    //在地圖上添加折線對象
    [_maMapView addOverlay:polygon];

- (MAOverlayView *)mapView:(MAMapView *)mapView viewForOverlay:(id )overlay
{
    if ([overlay isKindOfClass:[MAPolygon class]]) {
        _polygonView = [[MAPolygonView alloc] initWithPolygon:overlay];
        _polygonView.lineWidth = 5.f;
        _polygonView.strokeColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:0.8];
        _polygonView.fillColor = [UIColor colorWithRed:0.77 green:0.88 blue:0.94 alpha:0.8];
        //連接類型
        _polygonView.lineJoin = kCGLineJoinMiter;
        return _polygonView;
    }
    return nil;
}

點擊地圖區域判斷是否在范圍內
- (void)mapView:(MAMapView *)mapView didSingleTappedAtCoordinate:(CLLocationCoordinate2D)coordinate
{
    MAMapPoint point = MAMapPointForCoordinate(coordinate);
    if (MAPolygonContainsPoint(point, _polygonView.polygon.points, 4)) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"在區域內" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"返回" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"在區域內");
        }]];
        [self presentViewController:alert animated:YES completion:^{ }];
    }else{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"不在區域內" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"返回" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"不在區域內");
        }]];
        [self presentViewController:alert animated:YES completion:^{ }];
    }
}

就可以根據在范圍內和外進行分別的操作了

end

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved