IOS學(xué)習(xí)筆記之MKMapView的簡單使用

#import <MapKit/MapKit.h>
#import "ViewController.h"

@interface ViewController ()<MKMapViewDelegate>//導(dǎo)入代理
{
CLLocationManager* _locationManager;
}
@property (strong, nonatomic) IBOutlet UITextField *latitudeTF;
@property (strong, nonatomic) IBOutlet UITextField *longitudeTF;
@property (strong, nonatomic) IBOutlet MKMapView *mapV;
@property (strong, nonatomic) CLGeocoder *geocoder;

@end

@implementation ViewController

-(void)viewDidLoad {
    [super viewDidLoad];
    _locationManager = [[CLLocationManager alloc] init];
     // 需要在Info.plist文件  添加權(quán)限  Privacy - Location When In Use Usage Description 和 Privacy - Location Always Usage Description
    // 請求獲取定位授權(quán) 
    [_locationManager requestAlwaysAuthorization];
//    初始化地理編碼
    _geocoder = [[CLGeocoder alloc]init];
//    設(shè)置地圖標(biāo)準(zhǔn)類型
    _mapV.mapType = MKMapTypeStandard;
//    縮放
    _mapV.zoomEnabled = YES;
//    旋轉(zhuǎn)
    _mapV.rotateEnabled = YES;
//    滾動
    _mapV.scrollEnabled = YES;
//    交通流量
    _mapV.showsTraffic = YES;
//    指南針
    _mapV.showsCompass = YES;
    // 為MKMapView設(shè)置delegate
    _mapV.delegate = self;
    // 調(diào)用自己實(shí)現(xiàn)的方法設(shè)置地圖的顯示位置和顯示區(qū)域
    [self locateToLatitude:23.126272 longitude:113.395568];
//    / 創(chuàng)建一個手勢處理器肮柜,用于檢測椒振、處理長按手勢
    UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
//    手勢添加到視圖
    [self.view addGestureRecognizer:gesture];
    
}
//查詢按鈕實(shí)現(xiàn)方法
-(IBAction)getBtn:(UIButton *)sender {
    // 關(guān)閉兩個文本框的虛擬鍵盤
    [_latitudeTF resignFirstResponder];
    [_longitudeTF resignFirstResponder];
    NSString* latitudeStr = _latitudeTF.text;
    NSString* longtitudeStr =_longitudeTF.text;
    // 如果用戶輸入的經(jīng)度辛润、緯度不為空
    if (latitudeStr != nil && latitudeStr.length > 0
        && longtitudeStr != nil && longtitudeStr.length > 0){
        // 調(diào)用自己實(shí)現(xiàn)的方法設(shè)置地圖的顯示位置和顯示區(qū)域
        [self locateToLatitude:latitudeStr.floatValue
                     longitude:longtitudeStr.floatValue];
    }

}
//手勢實(shí)現(xiàn)方法
-(void)longPress:(UILongPressGestureRecognizer *)gesture{
    // 獲取長按點(diǎn)的坐標(biāo)
    CGPoint pos = [gesture locationInView:_mapV];
    // 將長按點(diǎn)的坐標(biāo)轉(zhuǎn)換為經(jīng)度值维哈、緯度值
    CLLocationCoordinate2D coord = [_mapV convertPoint:pos toCoordinateFromView:_mapV];

  //錨點(diǎn)和覆蓋層

   #if 1//錨點(diǎn)
    // 將經(jīng)度值、緯度值包裝為CLLocation對象
    CLLocation *location = [[CLLocation alloc]initWithLatitude:coord.latitude longitude:coord.longitude];
    // 根據(jù)經(jīng)度憔维、緯度反向解析地址
    [_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
       
        if (placemarks != nil && placemarks.count >0 && error==nil) {
             // 獲取解析得到的第一個地址信息
            CLPlacemark *placemark = placemarks[0];
            // 獲取地址信息中的FormattedAddressLines對應(yīng)的詳細(xì)地址
            NSArray* addrArray = placemark
            .addressDictionary[@"FormattedAddressLines"];
            // 將詳細(xì)地址拼接成一個字符串
            NSMutableString* address = [[NSMutableString alloc] init];
            for(int i = 0; i < addrArray.count; i ++){
                [address appendString:addrArray[i]];
            }
            // 創(chuàng)建MKPointAnnotation對象——代表一個錨點(diǎn)
            MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
            annotation.title = placemark.name;
            annotation.subtitle = address;
            annotation.coordinate = coord;
            //添加到地圖
            [_mapV addAnnotation:annotation];
            
        }
        
    }];
#elif 0 //覆蓋層
    // 創(chuàng)建MKCircle對象略板,該對象代表覆蓋層
    MKCircle* circle = [MKCircle circleWithCenterCoordinate:coord radius:50];
    // 添加MKOverlay 
    [_mapV addOverlay:circle level:MKOverlayLevelAboveLabels];
    
#endif
    
}

//自己定義定位方法
-(void)locateToLatitude:(CGFloat)latitude longitude:(CGFloat)longitude{
    // 設(shè)置地圖中心的經(jīng)度、緯度
    CLLocationCoordinate2D center = {latitude,longitude};
    // 設(shè)置地圖顯示的范圍边败,地圖顯示范圍越小袱衷,細(xì)節(jié)越清楚
    MKCoordinateSpan span = MKCoordinateSpanMake(0.005,0.005);
    // 創(chuàng)建MKCoordinateRegion對象,該對象代表地圖的顯示中心和顯示范圍
    MKCoordinateRegion region =MKCoordinateRegionMake(center, span);
    // 設(shè)置當(dāng)前地圖的顯示中心和顯示范圍
    [_mapV setRegion:region animated:YES];
    // 創(chuàng)建MKPointAnnotation對象——代表一個錨點(diǎn)
    MKPointAnnotation* annotation = [[MKPointAnnotation alloc] init];
    annotation.title = @"主標(biāo)題";
    annotation.subtitle = @"詳細(xì)地址";
    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(
                                                                   latitude , longitude);
    annotation.coordinate = coordinate;
    // 添加錨點(diǎn)
    [_mapV addAnnotation:annotation];
}
//渲染覆蓋層代理方法
-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay{
    MKCircle * circle = (MKCircle*)overlay;
    // 創(chuàng)建一個MKCircleRenderer對象
    MKCircleRenderer* render = [[MKCircleRenderer alloc] initWithCircle:circle];
    // 設(shè)置MKCircleRenderer的透明度
    render.alpha = 0.3;
    // 設(shè)置MKCircleRenderer的填充顏色和邊框顏色
    render.fillColor = [UIColor blueColor];
    render.strokeColor = [UIColor redColor];
    return render;
}
//正向解析
    NSString *addr = self.addrTF.text;
    if (addr != nil && addr.length > 0) {
        [geocoder geocodeAddressString:addr completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
            // 如果解析結(jié)果的集合元素的個數(shù)大于1笑窜,則表明解析得到了經(jīng)度致燥、緯度信息
            if (placemarks.count > 0) {
                CLPlacemark *placemark = placemarks[0];
                CLLocation *location = placemark.location;
                self.resultTF.text = [NSString stringWithFormat:@"%@的經(jīng)度為:%g 緯度為:%g",addr,location.coordinate.longitude,location.coordinate.latitude];
            }
            else
            {
                NSLog(@"地址無法解析");
            }
            
        }];
    }
//反向解析
  NSString *longitudeStr = _longitudeTF.text;;
    NSString *latitudeStr = _latitudeTF.text;
//    判斷不為空
    if (latitudeStr != nil && latitudeStr.length > 0 && longitudeStr !=nil && longitudeStr.length >0) {
        // 將用戶輸入的經(jīng)度、緯度封裝成CLLocation對象
        CLLocation *location = [[CLLocation alloc]initWithLatitude:latitudeStr.floatValue longitude:longitudeStr.floatValue];
        [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
            // 如果解析結(jié)果的集合元素的個數(shù)大于1排截,則表明解析得到了經(jīng)度嫌蚤、緯度信息
            if (placemarks.count > 0) {
                // 只處理第一個解析結(jié)果,實(shí)際項(xiàng)目可使用列表讓用戶選擇
                CLPlacemark *placemark = placemarks[0];
                // 獲取詳細(xì)地址信息
                NSArray *addrArray = placemark.addressDictionary[@"FormattedAddressLines"];
                // 將詳細(xì)地址拼接成一個字符串
                NSMutableString *addr = [[NSMutableString alloc]init];
                
                for (int i = 0; i < addrArray.count; i++) {
                    [addr appendString:addrArray[i]];
                }
                _resultTF.text = [NSString stringWithFormat:@"經(jīng)度為:%g,緯度為:%g的地址為%@",location.coordinate.longitude,location.coordinate.latitude,addr];
            }
            else
            {
                NSLog(@"您輸入的緯度無法解析");
            }
            
        }];
        
        
    }
    
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末断傲,一起剝皮案震驚了整個濱河市搬葬,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌艳悔,老刑警劉巖急凰,帶你破解...
    沈念sama閱讀 216,591評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡抡锈,警方通過查閱死者的電腦和手機(jī)疾忍,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,448評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來床三,“玉大人一罩,你說我怎么就攤上這事∑膊荆” “怎么了聂渊?”我有些...
    開封第一講書人閱讀 162,823評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長四瘫。 經(jīng)常有香客問我汉嗽,道長,這世上最難降的妖魔是什么找蜜? 我笑而不...
    開封第一講書人閱讀 58,204評論 1 292
  • 正文 為了忘掉前任饼暑,我火速辦了婚禮,結(jié)果婚禮上洗做,老公的妹妹穿的比我還像新娘弓叛。我一直安慰自己,他們只是感情好诚纸,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,228評論 6 388
  • 文/花漫 我一把揭開白布撰筷。 她就那樣靜靜地躺著,像睡著了一般畦徘。 火紅的嫁衣襯著肌膚如雪闭专。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,190評論 1 299
  • 那天旧烧,我揣著相機(jī)與錄音影钉,去河邊找鬼。 笑死掘剪,一個胖子當(dāng)著我的面吹牛平委,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播夺谁,決...
    沈念sama閱讀 40,078評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼廉赔,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了匾鸥?” 一聲冷哼從身側(cè)響起蜡塌,我...
    開封第一講書人閱讀 38,923評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎勿负,沒想到半個月后馏艾,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,334評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,550評論 2 333
  • 正文 我和宋清朗相戀三年琅摩,在試婚紗的時候發(fā)現(xiàn)自己被綠了铁孵。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,727評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡房资,死狀恐怖蜕劝,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情轰异,我是刑警寧澤岖沛,帶...
    沈念sama閱讀 35,428評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站搭独,受9級特大地震影響婴削,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜戳稽,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,022評論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望期升。 院中可真熱鬧惊奇,春花似錦、人聲如沸播赁。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,672評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽容为。三九已至乓序,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間坎背,已是汗流浹背替劈。 一陣腳步聲響...
    開封第一講書人閱讀 32,826評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留得滤,地道東北人陨献。 一個月前我還...
    沈念sama閱讀 47,734評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像懂更,于是被迫代替她去往敵國和親眨业。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,619評論 2 354

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