iOS高德地图SDK

作者: xxxixxxx | 来源:发表于2016-08-13 11:42 被阅读1608次

本文涉及到 定位 简单标注 多位置显示 导航

一、 安装不再讲述

二 、 使用

  1. 导入
#import <MAMapKit/MAMapKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
  1. 协议
<MAMapViewDelegate,AMapSearchDelegate>
  1. 声明
{
    ///地图视图
    MAMapView *_mapView;
    ///用户位置
    MAUserLocation *_userLocation;
}
  1. 用户定位具体实现
  - (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //设置key
    [AMapServices sharedServices].apiKey =@"";
    _mapView = [[MAMapView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 300)];
    _mapView.delegate = self;
    [self.view addSubview:_mapView];
    //考虑到可能单独要一个定位按钮
    UIButton *locationBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    locationBtn.frame = CGRectMake(0, 350, 60, 40);
    [locationBtn setTitle:@"定位" forState:UIControlStateNormal];
    [locationBtn addTarget:self action:@selector(locationClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:locationBtn];
   
    //开启定位
    [self locationClick];
}

 #pragma mark - 定位
  - (void)locationClick
{
    _mapView.showsUserLocation = YES;
    _mapView.userTrackingMode = MAUserTrackingModeFollowWithHeading;
    [_mapView setZoomLevel:16.1 animated:YES];
}
 #pragma mark 用户位置更新
 -(void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation
updatingLocation:(BOOL)updatingLocation
{
    if(updatingLocation)
    {
        //取出当前位置的坐标
        _userLocation = userLocation;
        NSLog(@"latitude : %f,longitude: %f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);
    }
}

 - (void)mapView:(MAMapView *)mapView didAddAnnotationViews:(NSArray *)views{
    MAAnnotationView *view = views[0];
    // 放到该方法中用以保证userlocation的annotationView已经添加到地图上了。
    if ([view.annotation isKindOfClass:[MAUserLocation class]])
    {
        MAUserLocationRepresentation *pre = [[MAUserLocationRepresentation alloc] init];
        pre.fillColor = [UIColor colorWithRed:0.9 green:0.1 blue:0.1 alpha:0.3];
        pre.strokeColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.9 alpha:1.0];
        pre.image = [UIImage imageNamed:@"location.png"];
        pre.lineWidth = 3;
        pre.lineDashPattern = @[@6, @3];
        //隐藏蓝色精度圈
        pre.showsAccuracyRing=NO;
        [_mapView updateUserLocationRepresentation:pre];
        
        view.calloutOffset = CGPointMake(0, 0);
    } 
 }

5 . 显示 用户位置 与 商家位置 (需实现4.)

#pragma mark - 直接显示商家位置 与用户位置
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
    pointAnnotation.coordinate = CLLocationCoordinate2DMake(28.698221, 115.849753);
    pointAnnotation.title = @"方恒国际";
    pointAnnotation.subtitle = @"阜通东大街6号";
    
    MAPointAnnotation *pointAnnotation2 = [[MAPointAnnotation alloc] init];
    pointAnnotation2.coordinate = CLLocationCoordinate2DMake(28.698220, 115.849759);
    
    NSArray *arr = @[pointAnnotation,pointAnnotation2];
    [_mapView addAnnotation:pointAnnotation];
    [_mapView showAnnotations:arr animated:YES];
    
}

#pragma mark 标注
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
        MAPinAnnotationView*annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
        if (annotationView == nil)
        {
            annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
        }
        annotationView.canShowCallout= YES;       //设置气泡可以弹出,默认为NO
        annotationView.animatesDrop = YES;        //设置标注动画显示,默认为NO
        annotationView.draggable = YES;        //设置标注可以拖动,默认为NO
        annotationView.image = [UIImage imageNamed:@"QQqq"];  //自定义标注(需将下面气泡注释)
        annotationView.pinColor = MAPinAnnotationColorPurple;
        return annotationView;
    }
    return nil;
}

导航相关

  1. 自己App内部地图界面导航

点我去官网查看路径规划
规划完路径开启导航

  1. 跳转到高德地图App内部导航

点我去官网查看

相关文章

网友评论

  • runsuc:高德地图打开会有这个Main Thread Checker: UI API called on a background thread: -[UIView bounds]提示吗
  • MyPolly快归来吧:大神,我想问下,这个是不是只是驾车的导航,如果是骑车导航,要改哪里呢
    xxxixxxx:@帅得人神共愤 不是跳转到高德App的话,你看下我上边刚更新的地址,看下官网的demo吧
    MyPolly快归来吧:@拾荒少年v 不是跳转的,就是用高德的SDK做骑行导航
    xxxixxxx:跳转app导航的话要看具体给出的参数
  • PGOne爱吃饺子:哥们 你的这个用户更新位置的这个方法为啥老是调用啊
    xxxixxxx:这个是实时更新的
  • 西门欥雪:高德地图2D版本是4.5.0,coocpods上最高版本4.3.0,不知道为什么cocopods上不能更新到4.5.0。。捉急
    xxxixxxx:@冷如冰寒如雪_西门吹雪 你在终端执行上述代码 更新Cocoapods 如果还没搜索到, 那就是高德还未更新
    西门欥雪:@拾荒少年v 在cocopods上pod search aMap ->4.3.0,在高德官网是几天更新的最新版本4.5.0,是高德地图的原因?还是cococpods没有识别最新的的版本号呢?大神。
    xxxixxxx:终端执行如下代码
    pod repo update

本文标题:iOS高德地图SDK

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