浏览器版本低!无法浏览完整内容,建议升级或更换浏览器。

iOS 地图SDK

百度地图SDK是一套应用程序接口, 通过调用地图SDK接口,开发者可以轻松访问百度地图的服务和数据,构建功能丰富、交互性强的地图类应用程序。

弧线

用户可以根据三个有序点唯一确定一条弧线,满足您的业务需求。

首先,修改您的.m文件,实现BMKMapViewDelegate的_mapView:viewForOverlay:函数,并在viewDidLoad添加弧线数据对象,核心代码如下:

    - (void)viewDidLoad {  
        [super viewDidLoad];  
    //添加弧线覆盖物  
    //传入的坐标顺序为起点、途经点、终点  
    CLLocationCoordinate2D coords[3] = {0};
        coords[0].latitude = 40.065;
        coords[0].longitude = 116.124;
        coords[1].latitude = 40.125;
        coords[1].longitude = 116.304;
        coords[2].latitude = 40.065;
        coords[2].longitude = 116.404; 
    BMKArcline *arcline = [BMKArcline arclineWithCoordinates:coords];  
       [_mapView addOverlay:arcline];  
 
    }  
    //根据overlay生成对应的View  
    - (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<bmkoverlay>)overlay  
    {  
    if ([overlay isKindOfClass:[BMKArcline class]])  
        {  
        BMKArclineView *arclineView = [[BMKArclineView alloc] initWithArcline:overlay];
        arclineView.strokeColor = [UIColor blueColor];
        arclineView.lineDash = YES;
        arclineView.lineWidth = 6.0;
        return arclineView; 
            }  
        return nil;  
    }

运行后,效果如图:

ios_map_draw_5.png

多边形

修改ViewController.h文件,使其实现BMKMapViewDelegate;同时修改ViewController.m文件,实现BMKMapViewDelegate的_mapView:viewForOverlay:函数,并在viewDidLoad添加多边形数据对象。核心代码如下:

- (void)viewDidLoad {     
    [super viewDidLoad];     
    // 添加多边形覆盖物    
            CLLocationCoordinate2D coords[5] = {0};
        coords[0].latitude = 39.965;
        coords[0].longitude = 116.604;
        coords[1].latitude = 39.865;
        coords[1].longitude = 116.604;
        coords[2].latitude = 39.865;
        coords[2].longitude = 116.704;
        coords[3].latitude = 39.905;
        coords[3].longitude = 116.654;
        coords[4].latitude = 39.965;
        coords[4].longitude = 116.704;
        polygon = [BMKPolygon polygonWithCoordinates:coords count:5]; 
    [_mapView addOverlay:polygon];    
}    
// Override    
- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay{  
    if ([overlay isKindOfClass:[BMKPolygon class]]){    
        BMKPolygonView* polygonView = [[BMKPolygonView alloc] initWithOverlay:overlay];
        polygonView.strokeColor = [[UIColor alloc] initWithRed:0.0 green:0 blue:0.5 alpha:1];
        polygonView.fillColor = [[UIColor alloc] initWithRed:0 green:1 blue:1 alpha:0.2];
        polygonView.lineWidth =2.0;
        polygonView.lineDash = YES;
		return polygonView;   
    }    
    return nil;  
}

运行后的显示效果如下图所示:

ios_map_draw_7.png

与多边形实现的方式类似。

首先要修改ViewController.h文件,实现BMKMapViewDelegate协议;然后修改ViewController.m文件,实现BMKMapViewDelegate的_mapView:viewForOverlay:函数,并在viewDidLoad添加圆数据对象。

核心代码如下:

- (void)viewDidLoad {     
    [super viewDidLoad];    
    // 添加圆形覆盖物    
    CLLocationCoordinate2D coor; 
    coor.latitude = 39.915; 
    coor.longitude = 116.404;
    BMKCircle* circle = [BMKCircle circleWithCenterCoordinate:coor radius:5000];
 
    [_mapView addOverlay:circle];    
}    
// Override    
- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay{    
    if ([overlay isKindOfClass:[BMKCircle class]]){    
        BMKCircleView* circleView = [[BMKCircleView alloc] initWithOverlay:overlay];    
        circleView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.5]; 
        circleView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.5];
        circleView.lineWidth = 10.0;
 
        return circleView;    
    }    
    return nil;   
}

运行效果如下图所示:

ios_map_draw_6.png