iOS_SN_百度地图基本使用(1)

上次用了一次百度地图,一直没有记笔记,今天记一笔。

以前没有用过百度地图的时候,听做这方面的朋友说百度地图有不少的坑,但是我做的时候没有遇到太大的坑,主要是要注意官方文档的注意事项,还有配置环境开发中的各个选项。

也不知道是什么原因,在配置plist 文件的时候做第一个demo的时候配置很成功,但是在实际的项目中不知道为什么那里没有配置好,重做了几次都没有做好,这里遇到的坑比较打吧,最后把配置成功的plist文件替换实际项目中的plist再把APP名字和bundle ID等字段替换,最后成功了。后面一看是因为下面一项没有配置好,是要在第一个配置里的字典添加NSAllowsArbitraryLoads这个选项而我是加载了外面这一层所以有点问题。

由于iOS9改用更安全的https,为了能够在iOS9中正常使用地图SDK,请在"Info.plist"中进行如下配置,否则影响SDK的使用。

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>

下面来看看基本地图:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, kwidth, kheigth)];

//    BMKMapTypeNone       = 0,               ///< 空白地图
//    BMKMapTypeStandard   = 1,               ///< 标准地图
//    BMKMapTypeSatellite  = 2,               ///< 卫星地图
    
    [_mapView setMapType:BMKMapTypeStandard];

    //打开实时路况图层
//    [_mapView setTrafficEnabled:YES];

    //关闭实时路况图层
//    [_mapView setTrafficEnabled:NO];

    //打开百度城市热力图图层(百度自有数据)
    [_mapView setBaiduHeatMapEnabled:YES];
    
    //关闭百度城市热力图图层(百度自有数据)
//    [_mapView setBaiduHeatMapEnabled:NO];
    
    [self.view addSubview:_mapView];

}

基本地图的使用其实就是地图的一些基本配置,按照自己需要的进行配置就行了。

下面是系统大头针的使用:

- (void) viewDidAppear:(BOOL)animated {
    // 添加一个PointAnnotation
    BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
    CLLocationCoordinate2D coor;
    coor.latitude = 39.915;
    coor.longitude = 116.404;
    annotation.coordinate = coor;
    annotation.title = @"这里是北京";
    [_mapView addAnnotation:annotation];
}


- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
    
    
//    不要标注的方法
//    if (annotation != nil) {
//        [_mapView removeAnnotation:annotation];
//    }
    
    
    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
        BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
        newAnnotationView.pinColor = BMKPinAnnotationColorPurple;
        newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示
        return newAnnotationView;
    }
    return nil;
}

 系统大头针的使用也简单,后面我会讲到自定义的大头针。

本文GitHub地址https://github.com/zhangkiwi/iOS_SN_BDMap-Test

原文地址:https://www.cnblogs.com/zhang-kiwi/p/5263443.html