iOS百度地图画一个矩形来显示矩形内的标注的个数(经纬度等等)

首先介绍下我们app:
由于我们是一个地图打点,连线的app。客户提出一个需求,在手机上画出一个矩形,要求计算矩形中的线总长度。

看到这个需求后,就去翻阅了百度sdk的文档: http://lbsyun.baidu.com/index.php?title=iossdk/guide/map-render/ploygon
百度地图sdk中,存在绘制面,弧线的api,但是都是需要经纬度之后才能绘制。 当时就放弃了这种方法。

  • 因为之前做app的时候 我们在mapview上有过view,然后转化为百度地图的gps坐标。所以觉得逻辑应该是,使用touch代理时间或者拖拽来在百度地图上画一个矩形view。然后将view的坐标转化为百度地图的坐标。这样子来确定经纬度范围。

所以:最终的方案是,先使用touch代理画取矩形(我使用的touch代理,没用拖拽手势)。然后将view贴到mapview上。转化为mapview的经纬度范围。再进行筛选。

一步一步实现:

  1. 先画一个view:
    自己做了一个小demo,通过touch的代理方法 来画出矩。下面看代码。

先看看代码块显示效果

PacView 是我自定义的一个view。 刚开始以为要用到drawrect。

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = _titleStr;
    self.view.backgroundColor = [UIColor whiteColor];
    _zyqview = [[PacView alloc]initWithFrame:CGRectMake(50, 104, 80, 80)];
    [self.view addSubview:_zyqview];
    _zyqview.backgroundColor = [UIColor greenColor];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    NSSet *allTouches = [event allTouches];    //返回与当前接收者有关的所有的触摸对象
    UITouch *touch = [allTouches anyObject];   //视图中的所有对象
    CGPoint point = [touch locationInView:[touch view]]; //返回触摸点在视图中的当前坐标
    _oriX = point.x;
    _oriY = point.y;
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesMoved:touches withEvent:event];
    NSSet *allTouches = [event allTouches];    //返回与当前接收者有关的所有的触摸对象
    UITouch *touch = [allTouches anyObject];   //视图中的所有对象
    CGPoint point = [touch locationInView:[touch view]]; //返回触摸点在视图中的当前坐标
    _changeX = point.x;
    _changeY = point.y;
    //    NSLog(@"2touch (x, y) is (%d, %d)", _changeX, _changeY);
    [self jisuanHeightWidth];
}
- (void)jisuanHeightWidth {
    int height = 0;
    int width = 0;
    
    if (_changeX > _oriX) {
        width = _changeX - _oriX;
    }else {
        width = _oriX - _changeX;
    }
    if (_changeY >_oriY) {
        height = _changeY - _oriY;
    }else {
        height = _oriY - _changeY;
    }
    
    CGRect rect = CGRectMake(0, 0, 0, 0);
    //右下
    if (_changeX > _oriX && _changeY >_oriY) {
        rect = CGRectMake(_oriX, _oriY, width, height);
    }
    //右上
    if (_changeX > _oriX && _changeY <_oriY) {
        rect = CGRectMake(_oriX, _changeY, width, height);
    }
    //左下
    if (_changeX < _oriX && _changeY >_oriY) {
        rect = CGRectMake(_changeX, _oriY, width, height);
    }
    //左上
    if (_changeX < _oriX && _changeY <_oriY) {
        rect = CGRectMake(_changeX, _changeY, width, height);
    }
    
    _zyqview.frame = rect;
}

以上代码就可以画出一个矩形了。 如果是拖拽方法的话,可能jisuanHeightWidth方法里的frame获取要改变下。

  1. 接入百度地图项目:

上面的画矩形方法直接扔到项目里就行了。

[self.view addSubview:_zyqview]; 这个方法的self.view 改为 mapview就行了。 因为一会你要转化为mapview的坐标经纬度

然后画完矩形后,手指松开,会走下面的方法

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    if (_isRightXY == YES) {
        [self caclulaterPacViewToMapView];

    }
    _isRightXY = NO;
    
}

- (void)caclulaterPacViewToMapView {
    if (_jisuanButton.selected == YES) {
        
        //百度地图
        BMKCoordinateRegion  region = [_mapView convertRect:_zyqview.frame toRegionFromView:_mapView];
//        region.span.latitudeDelta //纬度范围 是指总长,类似宽高。
//        region.span.longitudeDelta 经度范围
//        CLLocationCoordinate2D center;    ///< 中心点经纬度坐标
        double leftLat = region.center.latitude - region.span.latitudeDelta/2;
        double rightLat = region.center.latitude + region.span.latitudeDelta/2;
        double topLog = region.center.longitude - region.span.longitudeDelta/2;//最小
        double bootomLog = region.center.longitude + region.span.longitudeDelta/2;//最大
//        113.615697,34.745975;113.611570,34.750439  long,lat;long,lat
        
        
        //另一种计算方法
//        左下 右上 最小最大
        CGPoint mixPoint = CGPointMake(CGRectGetMinX(_zyqview.frame), CGRectGetMinY(_zyqview.frame)+CGRectGetHeight(_zyqview.frame));
        CGPoint maxPoint = CGPointMake(CGRectGetMinX(_zyqview.frame) + CGRectGetWidth(_zyqview.frame), CGRectGetMinY(_zyqview.frame));
        CLLocationCoordinate2D mixCoordinate = [_mapView convertPoint:mixPoint toCoordinateFromView:_mapView];

        CLLocationCoordinate2D maxCoordinate = [_mapView convertPoint:maxPoint toCoordinateFromView:_mapView];
        leftLat = mixCoordinate.latitude;
        rightLat = maxCoordinate.latitude;
        topLog = mixCoordinate.longitude;
        bootomLog = maxCoordinate.longitude;
        
        
        
        NSLog(@"圈圈圈%f,%f,%f,%f",topLog,bootomLog,leftLat,rightLat);
        NSMutableArray * counArray = [NSMutableArray new];
        for (TYBMKPolyline * line in _mapView.overlays) {
            if ([line isKindOfClass:[TYBMKPolyline class]]) {
                NSArray * latAndLogArray = [line.slnInfo.line_Longitude_Latitude componentsSeparatedByString:@";"];
                NSString * firstCor = latAndLogArray[0];//第一个coors
                NSString * secCor = latAndLogArray[1];//第2个coors
                
                double firstLog = [[firstCor componentsSeparatedByString:@","][0] doubleValue];
                double firstLat = [[firstCor componentsSeparatedByString:@","][1] doubleValue];
                double secLog = [[secCor componentsSeparatedByString:@","][0] doubleValue];
                double sectLat = [[secCor componentsSeparatedByString:@","][1] doubleValue];
                
                
                CLLocationCoordinate2D coors[2] = {0};
                coors[0].latitude = firstLat;
                coors[0].longitude = firstLog;
                coors[1].latitude = sectLat;
                coors[1].longitude = secLog;

                CLLocationCoordinate2D converTos[2] = {0};
                
                converTos[0] = [TYTool baiduLocationFromGPSCoordinate2D:coors[0]];
                converTos[1] = [TYTool baiduLocationFromGPSCoordinate2D:coors[1]];
                
                
                firstLat = converTos[0].latitude;
                firstLog = converTos[0].longitude;
                sectLat = converTos[1].latitude;
                secLog = converTos[1].longitude;
                
                double minLog = MIN(firstLog, secLog);
                double minLat = MIN(firstLat, sectLat);
                double maxLog = MAX(firstLog, secLog);
                double maxLat = MAX(firstLat, sectLat);
                NSLog(@"%f,%f,%f,%f \n ---%f",minLog,maxLog,minLat,maxLat,line.slnInfo.line_Distance);

                if (minLog>topLog && maxLog<bootomLog && minLat>leftLat && maxLat<rightLat) {//最大的和最大的比,最小的和最小的比
//                    NSLog(@"%f,%f,%f,%f ---%f",minLog,maxLog,minLat,maxLat,line.slnInfo.line_Distance);
                    [counArray addObject:line];
                }
                
            }

        }
//        NSLog(@"%ld",counArray.count);
        if (counArray.count != 0) {
            double allLineDis = 0.0;
            for (TYBMKPolyline * line in counArray) {
                allLineDis = allLineDis + line.slnInfo.line_Distance;
            }
            _tipsLabel.text = [NSString stringWithFormat:@"长度为%.f 米",allLineDis];
            
//            UIAlertController * ac = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"%f",allLineDis] preferredStyle:UIAlertControllerStyleAlert];
//            UIAlertAction * deleteAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
////                _zyqview.hidden = YES;
//            }];
//            [ac addAction:deleteAction];
//            [self presentViewController:ac animated:YES completion:nil];

        }else {
            [[TYNotification share] showPromptView:@"没有符合要求的线"];
            _tipsLabel.text = @"请划线";
//            _zyqview.hidden = YES;
        }
        
    }
}

关于百度sdk的这个方法:

BMKCoordinateRegion  region = [_mapView convertRect:_zyqview.frame toRegionFromView:_mapView];
//        region.span.latitudeDelta //纬度范围 是指总长,类似宽高。
//        region.span.longitudeDelta 经度范围
//        CLLocationCoordinate2D center;    ///< 中心点经纬度坐标
        double leftLat = region.center.latitude - region.span.latitudeDelta/2;
        double rightLat = region.center.latitude + region.span.latitudeDelta/2;
        double topLog = region.center.longitude - region.span.longitudeDelta/2;//最小
        double bootomLog = region.center.longitude + region.span.longitudeDelta/2;//最大
//        113.615697,34.745975;113.611570,34.750439  long,lat;long,lat

api上给的是 将view坐标 转化为经纬度。 region.span.longitudeDelta 经度范围,这个经度范围最开始 我当成是宽/2来计算。
double leftLat = region.center.latitude - region.span.latitudeDelta
后来发现根本不正确,精确度错的太多,后来web同事提醒,是不是region.span.longitudeDelta是宽度的一半,就改成了这样
double leftLat = region.center.latitude - region.span.latitudeDelta/2
后来发现还是不行,虽然精确度提高了,但是还是误差很大很大。
最后曲线救国吧

//        左下 右上 最小最大
        CGPoint mixPoint = CGPointMake(CGRectGetMinX(_zyqview.frame), CGRectGetMinY(_zyqview.frame)+CGRectGetHeight(_zyqview.frame));
        CGPoint maxPoint = CGPointMake(CGRectGetMinX(_zyqview.frame) + CGRectGetWidth(_zyqview.frame), CGRectGetMinY(_zyqview.frame));
        CLLocationCoordinate2D mixCoordinate = [_mapView convertPoint:mixPoint toCoordinateFromView:_mapView];

        CLLocationCoordinate2D maxCoordinate = [_mapView convertPoint:maxPoint toCoordinateFromView:_mapView];
        leftLat = mixCoordinate.latitude;
        rightLat = maxCoordinate.latitude;
        topLog = mixCoordinate.longitude;
        bootomLog = maxCoordinate.longitude;

将左下,右上的点提出来转化为经纬度,最后就ok了。

以上就是这次需求开发的坑。。

第一次用markdown写。gif不知道怎么上传=- =

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,490评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,581评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,830评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,957评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,974评论 6 393
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,754评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,464评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,357评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,847评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,995评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,137评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,819评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,482评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,023评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,149评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,409评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,086评论 2 355

推荐阅读更多精彩内容