美文网首页
iOS 百度地图 行政区域划分

iOS 百度地图 行政区域划分

作者: 路边的风景呢 | 来源:发表于2019-01-12 15:56 被阅读21次

最近 由于工作需要在百度地图的基础上要把后台反的某个 省 市 区 在地图上画出来 这个在百度的SDK上是添加覆盖物 在我这里只是简单的 用了一下 没有自定义之类的 都是从官方的demo 上面复制下来的可以放心用 

老套路  头文件

#import <BaiduMapAPI_Map/BMKMapComponent.h>

#import<BaiduMapAPI_Base/BMKBaseComponent.h>

#import <BaiduMapAPI_Search/BMKSearchComponent.h>

#import "BMKGeometry.h"  // 注意 这个一定要  不然下面的方法会报错 

首先要说一点 代理方法 千万不能忘记写 

<BMKMapViewDelegate,BMKDistrictSearchDelegate>

//地图的代理

 mapView.delegate=self;

//检索的代理

districtSearch.delegate=self

//下面是代码

 BMKMapView* mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 100)];

    self.mapView= mapView;

    mapView.delegate=self;

    self.mapView.zoomLevel = 10 ;

    [self.viewaddSubview:mapView];

    //初始化BMKDistrictSearch实例

    BMKDistrictSearch *districtSearch = [[BMKDistrictSearch alloc] init];

    //设置行政区域检索的代理

    districtSearch.delegate=self;

    //初始化请求参数类BMKDistrictSearchOption的实例

    BMKDistrictSearchOption *districtOption = [[BMKDistrictSearchOption alloc] init];

    //城市名,必选

    districtOption.city=@"北京市";

    //区县名字,可选

    districtOption.district=@"密云县";

    /**

     行政区域检索:异步方法,返回结果在BMKDistrictSearchDelegate的

     onGetDistrictResult里

     districtOption 公交线路检索信息类

     return 成功返回YES,否则返回NO

     */

    BOOLflag = [districtSearchdistrictSearch:districtOption];

    if(flag) {

        NSLog(@"行政区域检索发送成功");

    }else{

        NSLog(@"行政区域检索发送失败");

    }

//代理方法

#pragma mark - BMKDistrictSearchDelegate

/**

 行政区域检索结果回调

 @paramsearcher 检索对象

 @paramresult 行政区域检索结果

 @paramerror 错误码,@seeBMKCloudErrorCode

 */

- (void)onGetDistrictResult:(BMKDistrictSearch*)searcher result:(BMKDistrictResult*)result errorCode:(BMKSearchErrorCode)error {

    [_mapView removeOverlays:_mapView.overlays];

    //BMKSearchErrorCode错误码,BMK_SEARCH_NO_ERROR:检索结果正常返回

    if (error == BMK_SEARCH_NO_ERROR) {

        for(NSString*pathinresult.paths) {

            BMKPolygon *polygon = [self transferPathStringToPolygon:path];

            /**

             向地图View添加Overlay,需要实现BMKMapViewDelegate的-mapView:viewForOverlay:方法

             来生成标注对应的View

             @paramoverlay 要添加的overlay

             */

            [_mapViewaddOverlay:polygon];

        }

        _mapView.centerCoordinate = result.center;

        NSString*message = [NSStringstringWithFormat:@"行政区域编码:%ld\n行政区域名称:%@\n行政区域中心点:%f,%f", result.code, result.name, result.center.latitude,result.center.longitude];

        NSLog(@"%@",message);

    }

}

- (BMKPolygon*)transferPathStringToPolygon:(NSString*)path {

    NSUInteger pathCount = [path componentsSeparatedByString:@";"].count;

    if(pathCount >0) {

        BMKMapPointpoints[pathCount];

        NSArray *pointsArray = [path componentsSeparatedByString:@";"];

        for(NSUIntegeri =0; i < pathCount; i ++) {

            if([pointsArray[i]rangeOfString:@","].location!=NSNotFound) {

                NSArray*coordinates = [pointsArray[i]componentsSeparatedByString:@","];

                points[i] =BMKMapPointMake([coordinates.firstObjectdoubleValue], [coordinates .lastObjectdoubleValue]);

            }

        }

        /**

         根据多个点生成多边形

         points 直角坐标点数组,这些点将被拷贝到生成的多边形对象中

         count 点的个数

         新生成的多边形对象

         */

        BMKPolygon *polygon = [BMKPolygon polygonWithPoints:points count:pathCount];

        returnpolygon;

    }

    return nil;

}

#pragma mark - BMKMapViewDelegate

/**

 根据overlay生成对应的BMKOverlayView

 @parammapView 地图View

 @paramoverlay 指定的overlay

 @return生成的覆盖物View

 */

- (BMKOverlayView*)mapView:(BMKMapView*)mapView viewForOverlay:(id)overlay {

    if([overlayisKindOfClass:[BMKPolygonclass]]) {

        //初始化一个overlay并返回相应的BMKPolygonView的实例

        BMKPolygonView *polygonView = [[BMKPolygonView alloc] initWithOverlay:overlay];

        //设置polygonView的画笔(边框)颜色

        polygonView.strokeColor= [UIColorcolorWithRed:1green:0blue:0alpha:0.6];

        //设置polygonView的填充色

        polygonView.fillColor= [UIColorcolorWithRed:1green:1blue:0alpha:0.4];

        //设置polygonView的线宽度

        polygonView.lineWidth=1;

        //是否为虚线样式,默认NO

        polygonView.lineDash=YES;

        returnpolygonView;

    }

    return nil;

}

相关文章

  • iOS 百度地图 行政区域划分

    最近 由于工作需要在百度地图的基础上要把后台反的某个 省 市 区 在地图上画出来 这个在百度的SDK上是添加覆盖物...

  • RN-地图导航

    调起百度网页地图路径导航 调起高德网页地图路径导航 iOS调起百度APP地图路径导航 iOS调起高德app地图路径...

  • 地图集成调研

    地图集成调研 主要地图API有百度地图、高德地图、腾讯地图、搜狗地图(android、IOS暂不开放)。 百度地图...

  • 百度地图

    IOS第三方地图 百度地图高德地图腾讯地图苹果自带地图谷歌地图(中国屏蔽了) 接下来我们说一下百度地图百度地图SD...

  • 022-地图系列02-iOS百度地图版本及分类

    上一篇:021-地图系列01-iOS百度地图集成 一、iOS百度地图分类 按照大模块可以分为五大块 地图 定位 鹰...

  • 百度地图之官方demo的坑

    参考 iOS百度地图 Demo IOS SDK百度地图不能正常显示,只显示网格 刚下下来的是报错的,要执行以下几步...

  • 常用网址

    环信:http://www.easemob.com/customer/im IOS百度地图开发系列-百度地图不能正...

  • iOS百度地图使用时的注意问题

    iOS使用百度地图时会发现,API本身不带定位功能的,定位是通过iOS系统自身的定位实现的,百度地图API只是封装...

  • iOS百度地图反编码个人见解

    iOS百度地图反编码个人见解

  • 百度地图使用-大头针的自定义

    最近老看到iOS开发群里有人问有关百度地图的问题,正好研究过百度地图,今天我就把自己集成百度地图相关功能和遇到问题...

网友评论

      本文标题:iOS 百度地图 行政区域划分

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