iOS 地图的奇技淫巧

iOS高德地图SDK

2016-08-13  本文已影响1608人  xxxixxxx

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

一、 安装不再讲述

二 、 使用

  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内部导航

点我去官网查看

上一篇 下一篇

猜你喜欢

热点阅读