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