地图应用3《高德地图覆盖层》

#import#import "FKViewController.h"@interface FKViewController ()@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 *)mapViewrendererForOverlay:(id)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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容