高德地图集成总结

作者: 131413 | 来源:发表于2017-03-23 11:36 被阅读0次

    iOS开发,第一次集成高德地图,实现了简单的定位,绘制气泡,导航。简单总结:

    1.定位
    只是为了获取当前位置(如果需要展示地图可以直接使用MAMapView,不用设置定位管理器),使用AMapLocationManager类,需要自行配置,如定位精度,频率,地理反编码是否需要,是否需要持续定位等等,如果设置持续定位那么单次定位的配置和方法就会被覆盖。

    - (void)configLocationManager
    {
        self.locationManager = [[AMapLocationManager alloc] init];
        //代理方法 ,持续定位的获取,定位失败的处理等
        [self.locationManager setDelegate:self];
        
        [self.locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
        [self.locationManager setDistanceFilter:0];
    
        [self.locationManager setPausesLocationUpdatesAutomatically:YES];
        [self.locationManager setLocatingWithReGeocode:YES];
        //    [self.locationManager setAllowsBackgroundLocationUpdates:YES];
        
        [self.locationManager setLocationTimeout:3.0];
        [self.locationManager setReGeocodeTimeout:6.0];
        //开始连续定位
        [self.locationManager startUpdatingLocation];
        //单次定位的方法
        //    [self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
        //        <#code#>
        //    }];
    }
    

    2.展示地图
    跟系统的地图使用方法类似,可以自己配置显示地图类型,区域,比例,中心点等。

    - (void)initMapView{
        
        ///初始化地图
        _mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
        _mapView.delegate = self;
        
        _mapView.showsScale = YES;
        _mapView.showsCompass = YES;
        
        MACoordinateSpan span = MACoordinateSpanMake(0.1, 0.1);
        MACoordinateRegion region = MACoordinateRegionMake(_destinationCoordinate, span);
        _mapView.region = region;
        
        ///如果您需要进入地图就显示定位小蓝点,则需要下面两行代码
        _mapView.showsUserLocation = YES;
        _mapView.userTrackingMode = MAUserTrackingModeFollow;
        ///把地图添加至view
        _mapView.centerCoordinate = _destinationCoordinate;
    
        [self.view addSubview:_mapView];
    
    
    }
    

    3.标记和气泡
    即显示在地图上的大头针和大头针标注,默认的大头针可以自定义图片,气泡的自定义范围较小,如有需要可以继承MAPinAnnotationView,自定义属于自己的气泡即正常布局之后,在

    -(void)setSelected:(BOOL)selected animated:(BOOL)animated;
    中add到界面和赋值。

    注意,不要忘了在最后加上 [super setSelected:selected animated:YES],否则大头针和气泡的点击交互无效。

    具体聊聊在气泡添加点击事件遇到的坑:

    因为需求,需要在气泡上添加一个导航按钮,可在自定义气泡上加了点击事件之后,点击按钮响应的却是大头针的事件,经过仔细研究发现,其实大头针的视图范围很小,气泡视图看似正常显示在界面上,实际它的位置已经超出父视图,只是超出部分没有被切掉,所以当我们的点击根本传达不到按钮。
    解决方法
    重写-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event

     * @brief 系统用来判定交互是应该由哪个view响应
     * @param point 点击的位置,对于当前坐标系而言的
     * @event event 点击事件
     * @return 最佳响应事件的view
    - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
        UIView *view = [super hitTest:point withEvent:event];
        if (view == nil) {
            //当前坐标系上的点转换到按钮上的坐标
            CGPoint tempoint = [self.navBtn convertPoint:point fromView:self];
            if (CGRectContainsPoint(self.navBtn.bounds, tempoint))
            {
                view = self.navBtn;
            }
        }
        return view;
    }
    

    4.导航
    高德的导航非常简单,根据不同出行方式选择不同的导航管理器和导航地图即可。然后在管理器的代理方法中处理导航,具体可参考官方demo,很详细。

    以上全部仅供参考,不足之处欢迎指正。

    相关文章

      网友评论

        本文标题:高德地图集成总结

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