iOS拖動地圖選擇地點(diǎn)

項(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首頁

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,383評論 4 61
  • 《愛的五種語言》 從小就跟著奶奶去教堂,唱贊美詩,那時候的我識不全字,我總問奶奶,什么是愛? 奶奶把我摟入懷,問我...
  • 年少的時候看張愛玲的小說《傾城之戀》,總為“死生契闊,與子成說。執(zhí)子之手,與子偕老”這樣的誓言感動。覺得小說是講述...
    瀏如閱讀 305評論 0 0
  • 9月份了,史上更難就業(yè)季開始了,和一些學(xué)弟學(xué)妹聊,貌似今年產(chǎn)品經(jīng)理需求非常火爆,同時我發(fā)現(xiàn)他們對于產(chǎn)品經(jīng)理的理解比...
  • 級別,生活中常用的詞,在大級別的上漲中伴隨的是小級別的回抽,大級別的下跌中伴隨的是小級別的反彈。 交易,有...
    牛英栩閱讀 215評論 0 0

友情鏈接更多精彩內(nèi)容