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

在使用高德地圖之前需要現(xiàn)在高德地圖開放平臺創(chuàng)建一個APP趋翻,會自動生成一個appKey碍讯,在工程中使用高德地圖sdk時是需要這個key的,然后在自己的工程導(dǎo)入高德地圖sdk奕筐,高德地圖開放平臺上有詳細(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呈础,因為別的功能需要它的點擊事件)
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;
    
   //因為這個是后臺持續(xù)定位的代理方法蔬浙,所以必須停止定位猪落,否則地圖只要一移動就會回到當(dāng)前所在位置贞远。
   //因為一進(jìn)入地圖就會定位畴博,延遲1s停止定位,是為了能夠精準(zhǔn)的獲取到當(dāng)前位置蓝仲,否則可能會出現(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ū)域改變完成后會調(diào)用此接口(挪動地圖改變經(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)];
        
    }
}
上面用到的屬性(忘記寫在上面了,就補在這里)
@property (strong, nonatomic) MAMapView *mapView;
@property (strong, nonatomic) AMapLocationManager *locationManager;
@property (strong, nonatomic) AMapSearchAPI *searchPoi;
@property (strong, nonatomic) AMapReGeocodeSearchRequest *regeo;

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

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

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

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

下圖為運行后的圖片

結(jié)果圖片.jpg
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市促王,隨后出現(xiàn)的幾起案子犀盟,更是在濱河造成了極大的恐慌,老刑警劉巖蝇狼,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件阅畴,死亡現(xiàn)場離奇詭異,居然都是意外死亡迅耘,警方通過查閱死者的電腦和手機贱枣,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來颤专,“玉大人纽哥,你說我怎么就攤上這事⊙” “怎么了昵仅?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長累魔。 經(jīng)常有香客問我摔笤,道長,這世上最難降的妖魔是什么垦写? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任吕世,我火速辦了婚禮,結(jié)果婚禮上梯投,老公的妹妹穿的比我還像新娘命辖。我一直安慰自己,他們只是感情好分蓖,可當(dāng)我...
    茶點故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布尔艇。 她就那樣靜靜地躺著,像睡著了一般么鹤。 火紅的嫁衣襯著肌膚如雪终娃。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天蒸甜,我揣著相機與錄音棠耕,去河邊找鬼余佛。 笑死,一個胖子當(dāng)著我的面吹牛窍荧,可吹牛的內(nèi)容都是我干的辉巡。 我是一名探鬼主播,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼蕊退,長吁一口氣:“原來是場噩夢啊……” “哼郊楣!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起瓤荔,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤痢甘,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后茉贡,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體塞栅,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年腔丧,在試婚紗的時候發(fā)現(xiàn)自己被綠了放椰。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡愉粤,死狀恐怖砾医,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情衣厘,我是刑警寧澤如蚜,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站影暴,受9級特大地震影響错邦,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜型宙,卻給世界環(huán)境...
    茶點故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一撬呢、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧妆兑,春花似錦魂拦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至腺逛,卻和暖如春荷愕,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工路翻, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人茄靠。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓茂契,卻偏偏與公主長得像,于是被迫代替她去往敵國和親慨绳。 傳聞我的和親對象是個殘疾皇子掉冶,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,713評論 2 354

推薦閱讀更多精彩內(nèi)容