生活随笔
收集整理的這篇文章主要介紹了
MAP 地图
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
MAP 地圖
Main.storyboard 中設置控制器 導航 表格導入第三方 框架
1CoreLocation.framework
| 2MapKit.framework | |
viewcontrol
在這里插入代碼片//
// ViewController.m
// lyMap
//
// Created by 毅先森 on 2018/10/11.
// Copyright ? 2018 劉毅. All rights reserved.
//#import "ViewController.h"
//引入頭文件
#import "OneViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>//簽訂協議
{//定義省份數組NSArray *_provinceArr;
}
@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];//給省份數組初始化賦值_provinceArr = @[@"石家莊",@"廊坊",@"北京",@"保定",@"天津",@"衡水",@"張家口",@"邯鄲",@"雄安",@"安陽",@"洛陽"];
}
//表格 ‘行’
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{//返回數組的個數return _provinceArr.count;
}
//表格 ‘單元格’
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{//靜態字符串標識static NSString *str = @"cell";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];if (!cell) {cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];}//給cell賦值cell.textLabel.text = _provinceArr[indexPath.row];return cell;
}
//點擊cell進行跳轉進第二個界面
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{//得到點擊相對應的cell里面的文本NSString *str = _provinceArr[indexPath.row];//初始化第二個控制器OneViewController *one = [[OneViewController alloc]init];//給第二個界面傳參 '把得到點擊的cell的文本傳到one控制器的字符串passProvince里面'one.passProvince = str;//跳轉到第二個界面[self.navigationController pushViewController:one animated:YES];
}@end
4map viewcontrol.h 定義 屬性 接受數據 (copy)nsstring *.passProvince;
5 map viewcontrol
//
// OneViewController.m
// lyMap
//
// Created by 毅先森 on 2018/10/11.
// Copyright ? 2018 劉毅. All rights reserved.
//#import "OneViewController.h"
//引入頭文件 '地圖框架'
#import <MapKit/MapKit.h>
//引入頭文件 ‘定位框架’
#import <CoreLocation/CoreLocation.h>@interface OneViewController ()<MKMapViewDelegate,CLLocationManagerDelegate>//地圖視圖協議
//定義屬性 ‘地圖視圖’
@property(nonatomic,strong)MKMapView *MapView;
//定義屬性 ‘地理位置管理者’
@property(nonatomic,strong)CLLocationManager *locManager;@end@implementation OneViewController- (void)viewDidLoad {[super viewDidLoad];//初始化MKMapView ‘地圖視圖’self.MapView = [[MKMapView alloc]initWithFrame:self.view.frame];//設置地圖的樣式 'MKMapTypeSatellite衛星地圖' ‘MKMapTypeStandard平面地圖’self.MapView.mapType = MKMapTypeStandard;//簽訂協議self.MapView.delegate = self;//添加到視圖[self.view addSubview:self.MapView];//設置按鈕樣式 ‘UIButtonTypeRoundedRect圓角’UIButton *Btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];//獲取當前按鈕的位置Btn.frame = CGRectMake(10, self.view.frame.size.height-80, 40, 40);//設置按鈕的文字[Btn setTitle:@"定位" forState:UIControlStateNormal];//設置按鈕的顏色Btn.backgroundColor = [UIColor cyanColor];//給按鈕添加方法[Btn addTarget:self action:@selector(currentBtnDidPress:) forControlEvents:UIControlEventTouchUpInside];//添加到視圖上去[self.view addSubview:Btn];//初始化 地理位置管理者對象self.locManager = [[CLLocationManager alloc]init];//簽訂代理self.locManager.delegate = self;//申請用戶授權[self.locManager requestWhenInUseAuthorization];}
//獲取當前按鈕觸發
-(void)currentBtnDidPress:(id)sender{//開始獲取位置信息,調用此方法后,協議中的方法才會執行[self.locManager startUpdatingLocation];//通過位置管理者開始位置更新
}
//獲取當前位置信息后觸發的回調方法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{//獲取當前位置 CLLocation位置 locations位置信息一個數組,獲取最后一個最新的位置信息 大概CLLocation *cation = [locations lastObject];//獲取經緯度 coordinate坐標 ‘經緯度=當前位置的坐標’CLLocationCoordinate2D locationCoor = cation.coordinate;//輸出一下位置信息 longitude經度 latitude緯度NSLog(@"位置:經度:%g,緯度:%g",locationCoor.longitude,locationCoor.latitude);//讓地圖的顯示區縮小 MKCoordinateRegion顯示區 傳入一個經緯度 和兩個需要縮成的大小MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(locationCoor, 180, 180);//添加到地圖視圖上 給地圖縮小[self.MapView setRegion:region animated:YES];//反向地址解析,將經緯度轉換為字符串//初始化地址解析 ‘CLGeocoder地址解析’CLGeocoder *geocoder = [[CLGeocoder alloc]init];//解析 reverseGeocodeLocation傳入一個位置[geocoder reverseGeocodeLocation:cation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {//獲取其中的一個地址信息 j街道具體位置CLPlacemark *place = [placemarks lastObject];//做一個大頭針//初始化一個描點MKPointAnnotation *point = [[MKPointAnnotation alloc]init];//設置大頭針的標題 顯示當前的位置信息point.title = place.name;//設置大頭針顯示的經緯度 傳入locationCoor當前的經緯度point.coordinate = locationCoor;//添加到地圖上 必須是Annotation的類型[self.MapView addAnnotation:point];}];
}
//設置錨點視圖的回調方法,當地圖調用addAnnotation:方法時會觸發 點擊大頭針是觸發的方法
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{//靜態字符串標識static NSString *iden = @"pin";//初始化描點視圖MKPinAnnotationView *pin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:iden];//設置掉落狀態pin.animatesDrop = YES;//設置泡泡效果pin.canShowCallout = YES;return pin;
}@end
6 在Info.plist 添加
1 Privacy - Location Always and When In Use Usage Description
| 2 Privacy - Location Always Usage Description |
| 3 Privacy - Location When In Use Usage Description |
總結
以上是生活随笔為你收集整理的MAP 地图的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。