美文网首页
11.4 高德地图案列

11.4 高德地图案列

作者: 草根小强 | 来源:发表于2019-04-28 16:56 被阅读0次

高德地图案列

#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

相关文章

网友评论

      本文标题:11.4 高德地图案列

      本文链接:https://www.haomeiwen.com/subject/fnljnqtx.html