iOS拖动地图选择地点

作者: _Comma | 来源:发表于2017-02-20 16:36 被阅读1707次

项目中写了一个关于拖动地图选择位置的功能,日常记录一下 使用的是高德地图,这里只使用到了定位、地图和搜索的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首页

相关文章

  • iOS拖动地图选择地点

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

  • iOS 使用高德地图正确姿势(三)

    iOS 使用高德地图正确姿势(一)iOS 使用高德地图正确姿势(二) 实现大头针始终在地图中心,拖动地图实时poi...

  • [iOS] 实现中心大头针不随地图移动的思路

    在地图服务的应用场景中,有一个常见的需求是在地图中心放置一个大头针,让用户拖动地图,从地图上选择一个地点,要求:这...

  • iOS 地图不能拖动

    bug重现 当APP登录完毕进入地图界面之后,点击退出登录按钮,使用presentViewController:a...

  • ios scrollView 滑动与手势冲突

    问题描述: scrollview上添加了百度地图,百度地图的拖动与scrollview的上下拖动同时发生,左右拖动...

  • ios 地图如何区分人为拖动

    看网上,没找到相应的答案,说的大都是地图移动的一些代理方法,但是这个没法分辨出是否是用户手动操作的!正解如下: 试...

  • iOS地图的使用

    在iOS开发中要用到地图时选择很多,例如第三方的高德地图、百度地图都是很好的选择,当然最基础的则是iOS中自带的地...

  • iOS集成高德地图的地点名字选择

    最近的项目的功能中涉及到了地点的选择,需求上只需要搜索出具体的地名即可。当前项目只是应用于国内,也需要一个明确的地...

  • vue-amap 地图选点功能

    最近遇到一个地图相关需求,要求用户在地图上点击选择地点,地点可搜索,选择后带出坐标及具体位置,点标记只有一个,随着...

  • BottomSheetBehavior 重写

    为啥写这个,看到google地图的效果才想写的,下图是地图首次打开的界面 然后下边那部分是可以随意拖动的,拖动到哪...

网友评论

  • 浅笑回忆念旧时:大头针并不固定在中间
    _Comma:@浅笑回忆念旧时 :smile::smile::smile:
    浅笑回忆念旧时:@_Comma 我用的2D地图SDK,它不走- (void)mapViewRegionChanged:(MAMapView *)mapView 这个方法,所以并不固定在中间,不过问题已解决,感谢楼主分享
    _Comma:@浅笑回忆念旧时 大头针是一个图片放在地图上面图层,位置是自己定的
  • 6492983b777a:有没有demo,我也想参考参考,谢谢
    _Comma:@yihanyun 515385179,不好意思,以为写上去了
    6492983b777a:@_Comma 群是哪个?
    _Comma:@yihanyun 额,加一下群吧,就放群里了
  • Matsonga:您好 有demo吗 我们现在需要这个工能
    _Comma:@MaTsonga 你加下群吧,我应该丢群文件了
  • 方圆十里不留母狗:我的大头针为什么不会跟着走啊
    _Comma:@方圆十里不留母狗 不是让大头针走,而是让地图滚动,其实大头针一直没动过
  • bf514198fe7a:你这个用的是高德地图吗?方便发个demo吗?
    2fba0f5c7a44:@_Comma 有问题需要验证
    _Comma:@那天丶傍晚小池边 你加我QQ吧,之前没时间传git,506702341
  • 十一岁的加重:没有图片
    _Comma:@十一岁的加重 因为图片就是在地图中间放个大头针,然后拖地图选择大头针的位置,类似优步和滴滴那样吧
  • 0f452fa26796:很赞
    _Comma:@扎马斯 谢谢

本文标题:iOS拖动地图选择地点

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