高德地圖-始終獲取屏幕中心點(diǎn)的地址(或經(jīng)緯度)

在使用高德地圖之前需要現(xiàn)在高德地圖開放平臺(tái)創(chuàng)建一個(gè)APP,會(huì)自動(dòng)生成一個(gè)appKey,在工程中使用高德地圖sdk時(shí)是需要這個(gè)key的,然后在自己的工程導(dǎo)入高德地圖sdk,高德地圖開放平臺(tái)上有詳細(xì)的步驟。

1、引入需要的地圖框架
#import <MAMapKit/MAMapKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapSearchKit/AMapSearchKit.h>
#import <AMapLocationKit/AMapLocationKit.h>
2、創(chuàng)建地圖以及需要的屬性
//地圖
self.mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
self.mapView.delegate = self; //遵循代理<MAMapViewDelegate>
[self.view addSubview:self.mapView];

//定位
self.locationManager = [[AMapLocationManager alloc] init];
self.locationManager.delegate = self; //遵循代理<AMapLocationManagerDelegate>
[self.locationManager startUpdatingLocation];//開始定位

//逆地理編碼回調(diào)
self.regeo = [[AMapReGeocodeSearchRequest alloc] init];
self.searchPoi = [[AMapSearchAPI alloc] init];
self.searchPoi.delegate = self; //遵循代理<AMapSearchDelegate>

//設(shè)置地圖中間的圖片
if (self.centerImgView == nil)
{
     self.centerImgView = [[UIImageView alloc] initWithFrame:CGRectMake((kScreenWidth-20)/2, (kScreenHeight-37)/2, 20, 37)];
     self.centerImgView.image = [UIImage imageNamed:@"anno"];
}
[self.mapView addSubview:self.centerImgView];

//設(shè)置button,顯示當(dāng)前定位的地址(也可以用Label,因?yàn)閯e的功能需要它的點(diǎn)擊事件)
self.poiButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 40, kScreenWidth-40, 40)];
self.poiButton.backgroundColor = [UIColor colorWithRed:248/255.0 green:252/255.0 blue:255/255.0 alpha:1];
self.poiButton.layer.masksToBounds = YES;
self.poiButton.layer.cornerRadius = 20.0;
[self.poiButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
[self.mapView addSubview:self.poiButton];
3、定位代理方法
- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location
{
    //定位結(jié)果 
    NSLog(@"當(dāng)前經(jīng)緯度:location:{lat:%f; lon:%f}", location.coordinate.latitude, location.coordinate.longitude);
    //將當(dāng)前經(jīng)緯度設(shè)置為地圖的中心經(jīng)緯度
    self.mapView.centerCoordinate = location.coordinate;
    
   //因?yàn)檫@個(gè)是后臺(tái)持續(xù)定位的代理方法,所以必須停止定位,否則地圖只要一移動(dòng)就會(huì)回到當(dāng)前所在位置。
   //因?yàn)橐贿M(jìn)入地圖就會(huì)定位,延遲1s停止定位,是為了能夠精準(zhǔn)的獲取到當(dāng)前位置,否則可能會(huì)出現(xiàn)你定位在當(dāng)前位置了,但是button上顯示的卻是別的地方的位置
    double delayInSeconds = 1.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        //執(zhí)行事件
        [self.locationManager stopUpdatingLocation];//停止定位
    });
    
}
4、地圖區(qū)域改變完成后會(huì)調(diào)用此接口(挪動(dòng)地圖改變經(jīng)緯度)
- (void)mapView:(MAMapView *)mapView regionDidChangeAnimated:(BOOL)animated;
{
    self.centerCoor = [self.mapView convertPoint:self.mapView.center toCoordinateFromView:self.mapView];
    
//開始逆地理編碼,把當(dāng)前經(jīng)緯度轉(zhuǎn)成中文地址
    self.regeo.location  = [AMapGeoPoint locationWithLatitude:self.centerCoor.latitude longitude:self.centerCoor.longitude];
    [self.searchPoi AMapReGoecodeSearch:self.regeo];

}
5、逆地理編碼回調(diào)
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
{
    if (response.regeocode != nil)
    {
        
        //解析response獲取地址描述,可以自行選取需要的信息
        // self.nowStr 是由當(dāng)前經(jīng)緯度轉(zhuǎn)化的中文地址
        self.nowStr = [NSString stringWithFormat:@"%@%@%@%@", response.regeocode.addressComponent.province, response.regeocode.addressComponent.city, response.regeocode.addressComponent.district, response.regeocode.addressComponent.streetNumber.street];
         ;
        NSLog(@"??%@", self.nowStr);
        
        [self.poiButton setTitle:self.nowStr forState:(UIControlStateNormal)];
        
    }
}
上面用到的屬性(忘記寫在上面了,就補(bǔ)在這里)
@property (strong, nonatomic) MAMapView *mapView;
@property (strong, nonatomic) AMapLocationManager *locationManager;
@property (strong, nonatomic) AMapSearchAPI *searchPoi;
@property (strong, nonatomic) AMapReGeocodeSearchRequest *regeo;

//顯示中心點(diǎn)經(jīng)緯度的地址    
@property (strong, nonatomic) UIButton *poiButton;

//當(dāng)前位置地址
@property (strong, nonatomic) NSString *nowStr;

//中心點(diǎn)大頭針
@property (strong, nonatomic) UIImageView *centerImgView;

//中心點(diǎn)經(jīng)緯度結(jié)構(gòu)體
//這個(gè)就是當(dāng)前地圖中心店的經(jīng)緯度,如果需要使用這個(gè)經(jīng)緯度,可以直接用這個(gè)屬性
@property (assign, nonatomic) CLLocationCoordinate2D centerCoor;

下圖為運(yùn)行后的圖片

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

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

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