美文网首页
iOS 高德地图固定屏幕点

iOS 高德地图固定屏幕点

作者: buzaixian程序员 | 来源:发表于2018-09-04 13:26 被阅读248次
    先说一下实现的大致逻辑,大家可以看一下对自己是否有帮助的地方

    1、通过定位得到用户当前位置,创建annotation并设置属性让其固定并显示在地图中心点(这样会和MAUserLocation重合,我们要在代理方法里面去掉MAUserLocation和设置用户位置精度圈,就是蓝圈)
    2、自定义annotation图片
    3、在地图移动结束后的回调方法里面通过相对于地图的坐标得到当前的经纬度
    4、得到经纬度进行反地理编码,得到pois,展示列表
    5、点击列表,将地图的中心点移至点击对应的经纬度。

    代码:
    1、初始化mapView

        MAMapView *mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 300)];
        mapView.delegate = self;
        [self.view addSubview:mapView];
        
        // 定位当前位置
        mapView.userTrackingMode = MAUserTrackingModeFollow;
        mapView.showsUserLocation = YES;
        mapView.zoomLevel = 17;
    

    2、设置固定点

    /* 位置或者设备方向更新后,会调用此函数 */
    // 为了得到用户初始的位置,创建Annotation
    - (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation
    {
        // 目的是只用第一次定位的数据,即用户的初始位置
        if (self.userFirstLoca == NO) {
            self.userFirstLoca = YES;
            self.userFirstCoor = userLocation.location.coordinate;
            
            MAPointAnnotation *ann = [[MAPointAnnotation alloc] init];
            ann.coordinate = userLocation.location.coordinate;
           //固定Annotation并放置地图中心点
            ann.lockedToScreen = YES;
            ann.lockedScreenPoint = CGPointMake(mapView.center.x, mapView.center.y-64);
            [mapView addAnnotation:ann];
        }
    }
    

    3、一是去掉MAUserLocation的大头针,二是自定义设置固定Annotation的图片

    /* 根据anntation生成对应的View */
    -(MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
    {
        // 用户定位的annotation,置为空
        if ([annotation isKindOfClass:[MAUserLocation class]]) {
            MAAnnotationView *annView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"userLocationAnnViewID"];
            if (annView == nil) {
                annView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"userLocationAnnViewID"];
                annView.image = nil;
            }
            return annView;
        }else if ([annotation isKindOfClass:[MAPointAnnotation class]]) {
        // 固定Annotation自定义图片
            MAAnnotationView *annView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"annViewID"];
            if (annView == nil) {
                annView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annViewID"];
                annView.image = [UIImage imageNamed:@"endPoint@2x.png"];
            }
            return annView;
        }
        return nil;
    }
    

    4、地图移动结束后的回调方法里面通过相对于地图的坐标得到当前的经纬度。得到经纬度进行反地理编码,得到pois

    /* 地图移动结束后调用此接口 */
    - (void)mapView:(MAMapView *)mapView mapDidMoveByUser:(BOOL)wasUserAction
    {
        // 将指定view坐标系的坐标转换为经纬度
        CLLocationCoordinate2D moveCoor = [mapView convertPoint:CGPointMake(mapView.center.x, mapView.center.y-64) toCoordinateFromView:mapView];
        
        // 通过坐标点得到经纬度,从而进行反地理编码
        AMapReGeocodeSearchRequest *rego = [[AMapReGeocodeSearchRequest alloc] init];  
        // 是否返回扩展信(因为我们需要得到附近地理位置信息,所以设为YES)
        rego.requireExtension = YES;
        rego.location = [AMapGeoPoint locationWithLatitude:moveCoor.latitude longitude:moveCoor.longitude];
        [self.searchAPI AMapReGoecodeSearch:rego];
    }
    

    5、反地理编码,得到pois展示列表,点击列表,将地图的中心点移至点击对应的经纬度

    /* 逆地理编码查询回调函数 */
    - (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
    {
       // reGeodeVC就是一个简单的tableView布局,数据来源就是response.regeocode.pois
        [self.reGeodeVC reloadTableView:response.regeocode.pois];
    }
    
    /* 这个是reGeodeVC点击tableViewCell的回调 */
    #pragma mark - ReGeocodeDelegate
    -(void)selectAMapAPI:(AMapPOI *)poi
    {
        // 将地图的中心点移至poi点
        [self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(poi.location.latitude, poi.location.longitude) animated:YES];
    }
    

    另外:设置用户位置精度圈,去掉用户定位的蓝圈,可以自定义改变样式

        /* 是否自定义用户位置精度圈(userLocationAccuracyCircle)对应的 view, 默认为 NO.\n 如果为YES: 会调用 - (MAOverlayRenderer *)mapView (MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay 若返回nil, 则不加载.\n 如果为NO : 会使用默认的样式. */
        // 目的是去掉定位是的蓝圈,要在代理方法里面设置
        mapView.customizeUserLocationAccuracyCircleRepresentation = YES;
        /* 自定义用户位置精度圈触发的回调 */
    - (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay
    {
        // 判断是否为用户位置精度圈
        if (overlay == mapView.userLocationAccuracyCircle) {
            return nil;
        }
        return nil;
    }
    

    相关文章

      网友评论

          本文标题:iOS 高德地图固定屏幕点

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