iOS拖动地图选择地点

项目中写了一个关于拖动地图选择位置的功能,日常记录一下 使用的是高德地图,这里只使用到了定位、地图和搜索的SDK,直接上代码了

变量和懒加载

//定位
@property (nonatomic, strong) AMapLocationManager *locationManager;
//地图
@property (nonatomic, strong) MAMapView *mapView;
//大头针
@property (nonatomic, strong) MAPointAnnotation *annotation;
//逆地理编码
@property (nonatomic, strong) AMapReGeocodeSearchRequest *regeo;
//逆地理编码使用的
@property (nonatomic, strong) AMapSearchAPI *search;

- (AMapLocationManager *)locationManager {
    if (!_locationManager) {
        _locationManager = [[AMapLocationManager alloc]init];
        [_locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
        _locationManager.locationTimeout = 2;
        _locationManager.reGeocodeTimeout = 2;
    }
    return _locationManager;
}

- (AMapReGeocodeSearchRequest *)regeo {
    if (!_regeo) {
        _regeo = [[AMapReGeocodeSearchRequest alloc]init];
        _regeo.requireExtension = YES;
    }
    return _regeo;
}

- (AMapSearchAPI *)search {
    if (!_search) {
        _search = [[AMapSearchAPI alloc]init];
        _search.delegate = self;
    }
    return _search;
}

添加地图背景

//在viewDidLoad里面添加背景和定位功能
//地图
    _mapView = [[MAMapView alloc]initWithFrame:CGRectMake(0, 0, KSCREEN_WIDTH, KSCREEN_HEIGHT)];
    [self.view addSubview:_mapView];
    _mapView.showsUserLocation = YES;
    _mapView.delegate = self;
    _mapView.userTrackingMode = MAUserTrackingModeFollow;
    _mapView.showsScale = NO;

//定位
[self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
        
        if (error) {
            return ;
        }
        //添加大头针
        _annotation = [[MAPointAnnotation alloc]init];
        
        _annotation.coordinate = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude);
        [_mapView addAnnotation:_annotation];
        [_mapView setCenterCoordinate:CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude) animated:YES];
        //让地图在缩放过程中移到当前位置试图
        [_mapView setZoomLevel:16.1 animated:YES];
        
    }];

自定义大头针代理

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation {
    
    if ([annotation isKindOfClass:[MAPointAnnotation class]]) {
        static NSString *reuseIdetifier = @"annotationReuseIndetifier";
        MAAnnotationView *annotationView = (MAAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIdetifier];
        if (annotationView == nil) {
            annotationView = [[MAAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:reuseIdetifier];
        }
        //放一张大头针图片即可
        annotationView.image = [UIImage imageNamed:@"dingwei"];
        annotationView.centerOffset = CGPointMake(0, -18);
        return annotationView;
    }
    
    return nil;
}

地图开始移动和移动结束代理

#pragma mark - 让大头针不跟着地图滑动,时时显示在地图最中间
- (void)mapViewRegionChanged:(MAMapView *)mapView {
    _annotation.coordinate = mapView.centerCoordinate;
}
#pragma mark - 滑动地图结束修改当前位置
- (void)mapView:(MAMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    self.regeo.location = [AMapGeoPoint locationWithLatitude:mapView.centerCoordinate.latitude longitude:mapView.centerCoordinate.longitude];
    [self.search AMapReGoecodeSearch:self.regeo];
}

滑动结束调用search的代理进行逆地理编码得到地理位置

- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response {
    if (response.regeocode != nil) {
        AMapReGeocode *reocode = response.regeocode;
        //地图标注的点的位置信息全在reoceode里面了
    }
}
另外加个广告,推荐几个自己GitHub项目,希望多几个星星

UILabel分类,使用简单,动画改变label数值
对极光推送和信鸽推送的封装,统一调用,简单易懂
对MJRefresh二次封装,让代码更清晰
封装的一个二维码扫描器
登陆、支付、分享(待完善)功能封装
这是我的GitHub首页

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,705评论 4 61
  • 《爱的五种语言》 从小就跟着奶奶去教堂,唱赞美诗,那时候的我识不全字,我总问奶奶,什么是爱? 奶奶把我搂入怀,问我...
    梁明月创业笔记阅读 398评论 0 8
  • 年少的时候看张爱玲的小说《倾城之恋》,总为“死生契阔,与子成说。执子之手,与子偕老”这样的誓言感动。觉得小说是讲述...
    浏如阅读 301评论 0 0
  • 9月份了,史上更难就业季开始了,和一些学弟学妹聊,貌似今年产品经理需求非常火爆,同时我发现他们对于产品经理的理解比...
    小麦是产品经理阅读 1,288评论 1 39
  • 级别,生活中常用的词,在大级别的上涨中伴随的是小级别的回抽,大级别的下跌中伴随的是小级别的反弹。 交易,有...
    牛英栩阅读 214评论 0 0

友情链接更多精彩内容