11.4 高德地图案列

高德地图案列

#import "ViewController.h"
#import <MAMapKit/MAMapKit.h>
//搜索
#import <AMapSearchKit/AMapSearchKit.h>
//定位
#import <AMapLocationKit/AMapLocationKit.h>

@interface ViewController ()<MAMapViewDelegate,AMapSearchDelegate>
{
    MAMapView * _mapView;
    //搜索
    AMapSearchAPI * _searchAPI;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //配置key值
    [MAMapServices sharedServices].apiKey = @"aaaef3b9aa201997f1f6edeb39c4e15f";
    //创建地图
    [self createMapView];
    //添加自定义图层
    [self addCustomLayer];
    //画折线图
    [self drawLine];
    //画多边形
    [self drawPolygon];
    //画圆
    [self drawCircle];
    //搜索服务
    [self createSearch];
    
}

#pragma mark - 搜索服务
-(void)createSearch
{
    //配置搜索key
    [AMapSearchServices sharedServices].apiKey = @"aaaef3b9aa201997f1f6edeb39c4e15f";
    //初始化检索对象
    _searchAPI = [[AMapSearchAPI alloc]init];
    //设置代理
    _searchAPI.delegate = self;
    
    //类型一:POI搜索
    //构建搜索对象
    AMapPOIKeywordsSearchRequest * keywords = [[AMapPOIKeywordsSearchRequest alloc]init];
    //设置关键字
    keywords.keywords = @"肯德基";
    //设置城市
    keywords.city = @"北京";
    //发起搜索
    [_searchAPI AMapPOIKeywordsSearch:keywords];
    
    //类型二:路线规划
    //构造请求类,设置参数
    AMapDrivingRouteSearchRequest * drivingTouteRequest = [[AMapDrivingRouteSearchRequest alloc]init];
    //设置起点坐标
    drivingTouteRequest.origin = [AMapGeoPoint locationWithLatitude:39.994949 longitude:116.447265];
    //设置终点坐标
    drivingTouteRequest.destination = [AMapGeoPoint locationWithLatitude:39.990459 longitude:116.481476];
    //设置优先级
    drivingTouteRequest.strategy = MADrivingStrategyFastest;
    //设置返回扩展信息
    drivingTouteRequest.requireExtension = YES;
    
    //发起路径规划请求
    [_searchAPI AMapDrivingRouteSearch:drivingTouteRequest];
    
    //公交查询
    AMapBusStopSearchRequest * busStopRequest = [[AMapBusStopSearchRequest alloc]init];
    //设置关键字
    busStopRequest.keywords = @"老牛湾";
    //设置城市
    busStopRequest.city = @"北京";
    //发起请求
    [_searchAPI AMapBusStopSearch:busStopRequest];
}

#pragma mark - 搜索的代理方法
//POI的回调函数
-(void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response
{
    
}

//路径规划的回调函数
-(void)onRouteSearchDone:(AMapRouteSearchBaseRequest *)request response:(AMapRouteSearchResponse *)response
{
    
}
//公交站点查询
-(void)onBusStopSearchDone:(AMapBusStopSearchRequest *)request response:(AMapBusStopSearchResponse *)response
{
    
}

#pragma mark - 创建地图
-(void)createMapView
{
    _mapView = [[MAMapView alloc]initWithFrame:self.view.frame];
    //设置代理
    _mapView.delegate = self;
    [self.view addSubview:_mapView];
    //设置地图的类型,MAMapTypeSatellite表示卫星地图,MAMapTypeStandard表示标准地图,MAMapTypeStandardNight表示夜景地图
    _mapView.mapType = MAMapTypeStandard;
    //设置显示路况图
    _mapView.showTraffic = YES;
    //地图logo
    _mapView.logoCenter = CGPointMake(self.view.frame.size.width - 60, self.view.frame.size.height - 40);
    //指南针
    _mapView.showsCompass = YES;
    _mapView.compassOrigin =  CGPointMake(0, self.view.frame.size.height - 50);//指南针的位置
    //_mapView.compassSize 指南针的大小
    //比例尺
    _mapView.showsScale = YES;
    _mapView.scaleOrigin = CGPointMake(60, self.view.frame.size.height - 50);//比例尺的位置
    
    //地图手势
    _mapView.zoomEnabled = YES;//NO表示禁用手势
    //滑动手势
    _mapView.scrollEnabled = YES;
    //旋转手势
    _mapView.rotateEnabled = YES;
    //倾斜手势
    _mapView.rotateCameraEnabled = YES;
    
    //地图操作
    //设置缩放
    [_mapView setZoomLevel:18 animated:YES];
    
    //地图截屏
    CGRect inRect = _mapView.frame;
    UIImage *screenshotImage = [_mapView takeSnapshotInRect:inRect] ;
    
    UIImageView * imageView= [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    imageView.backgroundColor = [UIColor redColor];
    [self.view addSubview:imageView];
    imageView.image = screenshotImage;
    
    //显示用户的位置
    _mapView.showsUserLocation =YES;
    //设置模式
    _mapView.userTrackingMode = MAUserTrackingModeFollow;//跟随用户移动
    
}

#pragma mark - 添加自定义图层
-(void)addCustomLayer
{
    //确定中心位置
    _mapView.centerCoordinate = CLLocationCoordinate2DMake(39.910695, 116.372830);
    //设定zoomLevel
    _mapView.zoomLevel = 19;
    
    //构建自定义图层,传入的url参数是固定格式的
    MATileOverlay * overLay = [[MATileOverlay alloc]initWithURLTemplate:@"http://sdkdemo.amap.com:8080/tileserver/Tile?x={x}&y={y}&z={z}&f=2"];
    //设定最大最小值
    overLay.minimumZ = 18;
    overLay.maximumZ = 20;
    //设置可渲染的区域范围
    overLay.boundingMapRect = MAMapRectForCoordinateRegion(MACoordinateRegionMakeWithDistance(_mapView.centerCoordinate, 200, 200));
    
    //将图层添加到地图上
    [_mapView addOverlay:overLay];
    
}

#pragma mark - 画折线
-(void)drawLine
{
    CLLocationCoordinate2D coordinates[4];
    //第一个点
    coordinates[0].latitude = 39.832136;
    coordinates[0].longitude = 116.34095;
    
    //第二个点
    coordinates[1].latitude = 39.832136;
    coordinates[1].longitude = 116.42095;
    
    //第三个点
    coordinates[2].latitude = 39.902136;
    coordinates[2].longitude = 116.42095;
    
    //第四个点
    coordinates[3].latitude = 39.902136;
    coordinates[3].longitude = 116.44095;
    
    //构建折线对象,传入经纬度
    MAPolyline * polyLine = [MAPolyline polylineWithCoordinates:coordinates count:4];
    //将折现对象添加到地图上
    [_mapView addOverlay:polyLine];
}

#pragma mark - 画多边形
-(void)drawPolygon
{
    CLLocationCoordinate2D coordinates[4];
    //第一个点
    coordinates[0].latitude = 39.810892;
    coordinates[0].longitude = 116.233413;
    
    //第二个点
    coordinates[1].latitude = 39.816600;
    coordinates[1].longitude = 116.331842;
    
    //第三个点
    coordinates[2].latitude = 39.762187;
    coordinates[2].longitude = 116.357932;
    
    //第四个点
    coordinates[3].latitude = 39.733653;
    coordinates[3].longitude = 116.278255;
    
    //构建多边形对象,传入经纬度
    MAPolygon * polygon = [MAPolygon polygonWithCoordinates:coordinates count:4];
    //添加到地图上
    [_mapView addOverlay:polygon];
}

#pragma mark - 画圆
-(void)drawCircle
{
    //构造圆,radius是半径
    MACircle * circle = [MACircle circleWithCenterCoordinate:CLLocationCoordinate2DMake(39.952136,116.50095) radius:3000];
    //添加到地图上
    [_mapView addOverlay:circle];
}

#pragma mark - 地图的代理方法
//自定义图层的
-(MAOverlayView *)mapView:(MAMapView *)mapView viewForOverlay:(id<MAOverlay>)overlay
{
    //自定义图层
    //最终是将view添加到地图上
    if ([overlay isKindOfClass:[MAOverlayView class]]) {
        MAOverlayView * view = [[MAOverlayView alloc]initWithOverlay:overlay];
        return view;
    }
    
    //折线
    if ([overlay isKindOfClass:[MAPolyline class]]) {
        //构建一个view对象
        MAPolylineView * polyLineView = [[MAPolylineView alloc]initWithOverlay:overlay];
        //设置折线的颜色
        polyLineView.strokeColor = [UIColor yellowColor];
        //设置线宽
        polyLineView.lineWidth = 15;
        
        return polyLineView;
    }
    
    //多边形
    if ([overlay isKindOfClass:[MAPolygon class]]) {
        MAPolygonView * polygonView = [[MAPolygonView alloc]initWithOverlay:overlay];
        //线宽
        polygonView.lineWidth = 10;
        //线的颜色
        polygonView.strokeColor = [UIColor redColor];
        //填充颜色
        polygonView.fillColor = [UIColor cyanColor];
        
        return polygonView;
    }
    
    //画圆
    if ([overlay isKindOfClass:[MACircle class]]) {
        MACircleView * circleView = [[MACircleView alloc]initWithOverlay:overlay];
        //设置颜色
        circleView.strokeColor = [UIColor blueColor];
        //设置线宽
        circleView.lineWidth = 10;
        //设置填充色
        circleView.fillColor = [UIColor greenColor];
        //设置是否为虚线
        circleView.lineDash = YES;
        
        return circleView;
    }
    
    return nil;
}


#pragma mark - 定位的代理方法
//当位置更新的时候调用
-(void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation
{
    if (updatingLocation) {
        NSLog(@"经度%f~~~纬度%f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

高德地图案列.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,509评论 6 504
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,806评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,875评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,441评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,488评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,365评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,190评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,062评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,500评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,706评论 3 335
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,834评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,559评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,167评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,779评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,912评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,958评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,779评论 2 354

推荐阅读更多精彩内容