美文网首页
iOS 高德地图显示多边形围栏

iOS 高德地图显示多边形围栏

作者: 张天奇天张 | 来源:发表于2019-04-16 15:25 被阅读0次

#pragma mark - 地图上显示多边形

- (MKPolygon*)showPolygonInMap:(CLLocationCoordinate2D*)coordinates count:(NSInteger)count {

    MKPolygon *polygonOverlay = [MKPolygon polygonWithCoordinates:coordinates count:count];

    [self.mapView addOverlay:polygonOverlay];

    return polygonOverlay;

}

#pragma mark - 清除上一次按钮点击创建的围栏

- (void)doClear {

    [self.mapView removeOverlays:self.mapView.overlays];  //把之前添加的Overlay都移除掉

    [self.geoFenceManager removeAllGeoFenceRegions];  //移除所有已经添加的围栏,如果有正在请求的围栏也会丢弃

}

#pragma mark - 添加多边形围栏

- (void)GeoFence{

    NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];

    [[NetworkManager sharedManager] POSTWithURLString:ElectronFence parameters:parameters success:^(id responseObject) {

        NSDictionary *userDic = responseObject[@"result"];

        NSArray* fenceList = userDic[@"fenceList"];

        [fenceList enumerateObjectsUsingBlock:^(NSDictionary * dic, NSUInteger idx, BOOL * _Nonnull stop) {

            NSArray *array = dic[@"detail"];

            CLLocationCoordinate2D *coorArr = malloc(sizeof(CLLocationCoordinate2D) * array.count);

            for(inti =0; i < array.count; i++) {

                CGFloatlongitude = [array[i][@"longitude"] doubleValue];

                CGFloatlatitude = [array[i][@"latitude"] doubleValue];

                coorArr[i] = CLLocationCoordinate2DMake(latitude, longitude);

            }

            [self doClear];

            self.geoFenceManager.activeAction = AMapGeoFenceActiveActionInside | AMapGeoFenceActiveActionOutside | AMapGeoFenceActiveActionStayed; //监听进入、退出、停留事件

            [self.geoFenceManager addPolygonRegionForMonitoringWithCoordinates:coorArr count:array.count customID:@"polygon_1"];

            free(coorArr);  //malloc,使用后,记得free

            coorArr =NULL;

        }];

    }failure:^(NSError*error) {

        NSLog(@"%@",error);

        [self doClear];

        self.geoFenceManager.activeAction = AMapGeoFenceActiveActionInside | AMapGeoFenceActiveActionOutside | AMapGeoFenceActiveActionStayed; //监听进入、退出、停留事件

    }];

}

#pragma mark - 添加地理围栏完成后的回调

- (void)amapGeoFenceManager:(AMapGeoFenceManager*)manager didAddRegionForMonitoringFinished:(NSArray *)regions customID:(NSString*)customID error:(NSError*)error {

    if([customIDisEqualToString:@"polygon_1"]){

        if(error) {

            NSLog(@"=======polygon error %@",error);

        }else{  //围栏添加后,在地图上的显示,只是为了更方便的演示,并不是必须的.

            AMapGeoFencePolygonRegion *polygonRegion = (AMapGeoFencePolygonRegion *)regions.firstObject;

            MKPolygon *polygonOverlay = [self showPolygonInMap:polygonRegion.coordinatescount:polygonRegion.count];

            [self.mapView setVisibleMapRect:polygonOverlay.boundingMapRect edgePadding:UIEdgeInsetsMake( -50, 120, 120, 120) animated:NO];

        }

    }

}

#pragma mark - 地理围栏状态改变时回调

- (void)amapGeoFenceManager:(AMapGeoFenceManager*)manager didGeoFencesStatusChangedForRegion:(AMapGeoFenceRegion*)region customID:(NSString*)customID error:(NSError*)error {

    if(error) {

                NSLog(@"status changed error %@",error);

    }else{

                NSLog(@"status changed %@",[regiondescription]);

    }

    switch(region.fenceStatus) {

        case AMapGeoFenceRegionStatusInside:

        {

            //在区域内

            NSLog(@"在 区域内");

        }

            break;

        case AMapGeoFenceRegionStatusOutside:

        {

            //在区域外

            NSLog(@"在 区域外");

            /////围栏报警超出区域

                [self player];

        }

            break;

        case AMapGeoFenceRegionStatusStayed:

        {

            //停留超过十分钟

        }

            break;

        default: {

            //未知

          NSLog(@"在 未知");

        }

            break;

    }

}

- (void) player {

    //本地音乐播放

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"超出围栏(王老师)" ofType:@"mp3"];

    NSURL*url = [NSURL fileURLWithPath:filePath];

    palyer = [[AVPlayer alloc] initWithURL:url];

    [palyer play];

}

相关文章

网友评论

      本文标题:iOS 高德地图显示多边形围栏

      本文链接:https://www.haomeiwen.com/subject/tkpbwqtx.html