Oc 地图覆盖层

本文主要介绍了在iOS中如何使用MapKit框架的MKMapView来创建和管理地图覆盖层,实现自定义地图显示效果。
摘要由CSDN通过智能技术生成

屏幕快照 2017-08-05 08.05.00.png

实现代码

#import <MapKit/MapKit.h>
#import "FKViewController.h"

@interface FKViewController () <MKMapViewDelegate>
@property (nonatomic, strong) MKMapView* mapView;
@end
@implementation FKViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
    self.mapView.zoomEnabled = YES;
    // 设置地图可滚动
    self.mapView.scrollEnabled = YES;
    // 设置地图不可旋转
    self.mapView.rotateEnabled = NO;
    // 设置地图显示用户位置
    self.mapView.showsUserLocation = YES;
    [self.view addSubview:self.mapView];

    [self locateToLatitude:23.126272 longitude:113.395568];
    // 创建一个手势处理器,用于检测、处理长按手势
    UILongPressGestureRecognizer* gesture = [[UILongPressGestureRecognizer
        alloc]initWithTarget:self action:@selector(longPress:)];

    [self.mapView addGestureRecognizer:gesture];
    self.mapView.delegate = self;
}
- (void)locateToLatitude:(CGFloat)latitude longitude:(CGFloat)longitude
{
    // 设置地图中心的经、纬度
    CLLocationCoordinate2D center = {latitude , longitude};
    // 设置地图显示的范围,
    MKCoordinateSpan span;
    // 地图显示范围越小,细节越清楚
    span.latitudeDelta = 0.01;
    span.longitudeDelta = 0.01;
    // 创建MKCoordinateRegion对象,该对象代表了地图的显示中心和显示范围。
    MKCoordinateRegion region = {center,span};
    // 设置当前地图的显示中心和显示范围
    [self.mapView setRegion:region animated:YES];
}
- (void) longPress:(UILongPressGestureRecognizer*)gesture
{
    // 获取长按点的坐标
    CGPoint pos = [gesture locationInView:self.mapView];
    // 将长按点的坐标转换为经度、维度值
    CLLocationCoordinate2D coord = [self.mapView convertPoint:pos
        toCoordinateFromView:self.mapView];
    // 创建MKCircle对象,该对象代表覆盖层

    MKCircle* circle = [MKCircle circleWithCenterCoordinate:coord radius:100];

    // 添加MKOverlay
    [self.mapView addOverlay:circle level:MKOverlayLevelAboveLabels];
}
// MKMapViewDelegate协议中的方法,该方法返回的MKOverlayRenderer负责绘制覆盖层控件
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView
    rendererForOverlay:(id<MKOverlay>)overlay
{
    MKCircle * circle = (MKCircle*)overlay;
    // 创建一个MKCircleRenderer对象
    MKCircleRenderer* render = [[MKCircleRenderer alloc] initWithCircle:circle];
    // 设置MKCircleRenderer的透明度
    render.alpha = 0.5;
    // 设置MKCircleRenderer的填充颜色和边框颜色
    render.fillColor = [UIColor blueColor];
    render.strokeColor = [UIColor redColor];
    return render;
}
@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值