美文网首页
iOS集成高德地图SDK

iOS集成高德地图SDK

作者: 一个不太努力的代码搬运工 | 来源:发表于2017-08-01 14:44 被阅读0次

    前言:关于这次集成高德地图,打算分几个内容定位 POI检索 导航 线路规划,现在只是简单地实现了前两个功能,先记录一下吧。
    至于集成SDK的过程很简单,需要注意的只是Info.plist中的添加两个字段
    Privacy - Location When In Use Usage Description 定位权限(使用时定位)
    Allow Arbitrary Loads

    • 定位

    (1)添加大头针
    先导入头文件
    #import <MAMapKit/MAMapKit.h> #import <AMapFoundationKit/AMapFoundationKit.h> #import <AMapSearchKit/AMapSearchKit.h>
    懒加载创建mapView

        if (_mapView == nil) {
            _mapView = [[MAMapView alloc]initWithFrame:self.view.bounds];
            //设置进入地图的缩放比例
            [_mapView setZoomLevel:17.5 animated:YES];
            //地图跟着位置移动
            [_mapView setUserTrackingMode: MAUserTrackingModeFollow animated:NO];
            ///如果您需要进入地图就显示定位小蓝点,则需要下面两行代码
            _mapView.showsUserLocation = YES;
            //如果不写这句话,就不会显示蓝色圆圈,也不会自动定位,这个貌似无法更改大小
            _mapView.userTrackingMode = MAUserTrackingModeFollow;
            _mapView.delegate = self;
        }
        return _mapView;
    }
    

    创建大头针重要的一点是要实现-(MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation代理方法
    此时我们并没有给大头针加上经纬度和标题,看很多demo都是直接赋值,并没有根据当前用户的位置设置大头针,经过我不断地踩坑,解决了这个问题。
    要获取当前用户的位置及位置的title和subTitle,要实现<AMapSearchKit/AMapSearchKit.h>中的两个代理方法:
    // 用户地址发生更新后执行- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation(这个方法用于获取当前用户的经纬度)
    // 反编码回调函数- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response(这个方法用于获取当前位置的title和subTitle)
    此时我们就可以使用大头针的类MAPointAnnotation,创建一个全局对象,在代理方法中对其设置

    - (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation {
        //将用户地址传入参数currentLocation
        currentLocation = [userLocation.location copy];
        //获得当前地理编码后,进行反编码,获得当前定位点的信息
        [self reGeoAction];
        
    }
    //地理反编码函数
    -(void)reGeoAction {
        if (currentLocation) {
            //生成一个地理反编码的搜索
            AMapReGeocodeSearchRequest *request = [[AMapReGeocodeSearchRequest alloc] init];
            
            request.location = [AMapGeoPoint locationWithLatitude:currentLocation.coordinate.latitude longitude:currentLocation.coordinate.longitude];
            [self.searchAPI AMapReGoecodeSearch:request];
        }
    }
    // 反编码回调函数
    - (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response {
        //将搜索结果传给用户定位点,这样点击气泡就可以显示出详细信息
        NSString *title = response.regeocode.addressComponent.city;
        if (title.length ==0) {
            title = response.regeocode.addressComponent.province;
        }
        self.mapView.userLocation.title = title;
        self.mapView.userLocation.subtitle = response.regeocode.formattedAddress;
        [self.mapView selectedAnnotations];
        self.mapView.centerCoordinate = CLLocationCoordinate2DMake(currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
        pointAnnotation.coordinate = CLLocationCoordinate2DMake(currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
        pointAnnotation.title = title;
        pointAnnotation.subtitle = response.regeocode.formattedAddress;
    
    }
    
    
    //添加大头针
    -(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.pinColor = MAPinAnnotationColorPurple;
            return annotationView;
        }
        return nil;
    }
    

    效果图(最好用真机测试,模拟器不准确):

    定位大头针

    (2)添加覆盖圆

    - (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response {
     
        MACircle *circle = [MACircle circleWithCenterCoordinate:currentLocation.coordinate radius:1000];
        [self.mapView addOverlay:circle];
    }
    
    //添加覆盖的圆圈的代理方法
    -(MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay{
        
        if ([overlay isKindOfClass:[MACircle class]])
        {
            MACircleRenderer *circleView = [[MACircleRenderer alloc] initWithCircle:overlay];
            circleView.lineWidth = 5.f;
            circleView.strokeColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:0.8];
            circleView.fillColor = [UIColor colorWithRed:1.0 green:0.8 blue:0.0 alpha:0.8];
            return circleView;
        }
        return nil;
    }
    
    • POI周边检索

      自定义一个搜索框,将搜索框中的字符串作为POI检索的关键字,然后将搜索到的信息展示出来,实现过程很简单,先上一张效果图:
      POI检索效果图:POI检索效果图:
      )
      效果图直接去github上看吧,简书不知道为什么上传不了动图了.
      Demo地址

    相关文章

      网友评论

          本文标题:iOS集成高德地图SDK

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