項(xiàng)目中寫了一個關(guān)于拖動地圖選擇位置的功能,日常記錄一下 使用的是高德地圖,這里只使用到了定位、地圖和搜索的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];
//讓地圖在縮放過程中移到當(dāng)前位置試圖
[_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;
}
地圖開始移動和移動結(jié)束代理
#pragma mark - 讓大頭針不跟著地圖滑動,時時顯示在地圖最中間
- (void)mapViewRegionChanged:(MAMapView *)mapView {
_annotation.coordinate = mapView.centerCoordinate;
}
#pragma mark - 滑動地圖結(jié)束修改當(dāng)前位置
- (void)mapView:(MAMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
self.regeo.location = [AMapGeoPoint locationWithLatitude:mapView.centerCoordinate.latitude longitude:mapView.centerCoordinate.longitude];
[self.search AMapReGoecodeSearch:self.regeo];
}
滑動結(jié)束調(diào)用search的代理進(jìn)行逆地理編碼得到地理位置
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response {
if (response.regeocode != nil) {
AMapReGeocode *reocode = response.regeocode;
//地圖標(biāo)注的點(diǎn)的位置信息全在reoceode里面了
}
}
另外加個廣告,推薦幾個自己GitHub項(xiàng)目,希望多幾個星星
UILabel分類,使用簡單,動畫改變label數(shù)值
對極光推送和信鴿推送的封裝,統(tǒng)一調(diào)用,簡單易懂
對MJRefresh二次封裝,讓代碼更清晰
封裝的一個二維碼掃描器
登陸、支付、分享(待完善)功能封裝
這是我的GitHub首頁