前言:讓有需要的人看一眼,如果能得到一些自己需要的,我將倍感榮幸.
注意###
如果用storyBoard的mapView要導(dǎo)入MapKit的framework.因?yàn)閄code5以后,系統(tǒng)會(huì)自己導(dǎo)入框架,但是storyBoard要先于代碼執(zhí)行.所以要先導(dǎo)入框架.
如果一切弄好了,發(fā)現(xiàn)定位錯(cuò)了,改一下當(dāng)前定位的位置
simulator的Debug->location->customer location -> 39,116(隨便寫,別寫出國(guó)去,臺(tái)灣是中國(guó)的一個(gè)省)
可以cmd R 運(yùn)行一下 看一眼 然后進(jìn)行下面的代碼
設(shè)置地圖類型###
//地圖視圖
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
/*
//標(biāo)準(zhǔn)
MKMapTypeStandard = 0,
//衛(wèi)星
MKMapTypeSatellite,
//鳥瞰
MKMapTypeHybrid,
//以下兩個(gè)暫時(shí)國(guó)內(nèi)沒用到
MKMapTypeSatelliteFlyover ,
MKMapTypeHybridFlyover ,
*/
self.mapView.mapType = MKMapTypeStandard;
//實(shí)時(shí)交通狀況
// Affects MKMapTypeStandard and MKMapTypeHybrid(意思是只能在標(biāo)準(zhǔn)和鳥瞰下使用)
self.mapView.showsTraffic = true;
//顯示用戶位置
/*
不跟蹤
MKUserTrackingModeNone = 0,
跟蹤
MKUserTrackingModeFollow,
有方向的跟蹤
MKUserTrackingModeFollowWithHeading
*/
self.mapView.userTrackingMode = MKUserTrackingModeFollow;
//定位必須授權(quán) 別忘了info.plist文件,key不要寫錯(cuò)
self.locationManager = [CLLocationManager new];
[self.locationManager requestWhenInUseAuthorization];
//有可能用模擬器出不來交通狀況,用真機(jī)則沒有這些情況,應(yīng)該是模擬器的一些bug.
點(diǎn)擊定位的藍(lán)色光圈顯示真實(shí)位置###
現(xiàn)在點(diǎn)擊藍(lán)色光圈,顯示的是My Location 不想要這個(gè),想要具體位置
我在代碼塊里面標(biāo)注123,顯示的會(huì)清晰一些 所用代碼還是上面的代碼塊,進(jìn)行添加
//點(diǎn)擊藍(lán)色光圈 顯示具體位置 步驟
//1.設(shè)置地圖代理
self.mapView.delegate = self;
//2.遵循協(xié)議 <MKMapViewDelegate>
//3.實(shí)現(xiàn)方法
//代理方法---定位到用戶位置 會(huì)調(diào)用(也就是userLocation -->大頭針(有時(shí)間 會(huì)另起一章寫一下看心情= =))
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
//點(diǎn)擊MKUserLocation 進(jìn)去找到 title和subTitle的屬性 先設(shè)置一下,看看效果如何
// userLocation.title = @"北京市";
// userLocation.subtitle = @"天安門";
//可以進(jìn)行修改,查找正確
//cmd 點(diǎn)擊 MKUserLocation 進(jìn)去再查找所需要屬性 發(fā)現(xiàn) @property (readonly, nonatomic, nullable) CLLocation *location; 有位置 可以進(jìn)行反地理編碼
//反地理編碼 步驟走一波
//1.創(chuàng)建對(duì)象
CLGeocoder *gecoder = [CLGeocoder new];
//2.調(diào)用方法
[gecoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
//防錯(cuò)
if (error) {
NSLog(@"%@",error);
return;
}
//賦值
userLocation.title = placemarks[0].locality;
userLocation.subtitle = placemarks[0].name;
}];
}
//當(dāng)網(wǎng)速慢的時(shí)候,要等一會(huì),如果就是不想要My Location就把那兩個(gè)title設(shè)置為正在加載,怎么開心怎么玩.
點(diǎn)擊按鈕 返回定位位置###
玩過地圖的都知道 你找地方的時(shí)候,有個(gè)按鈕 點(diǎn)一下就能回到最初的定位位置
用此方法可以回到當(dāng)前位置,前提不放大不縮小 因?yàn)榉椒〞?huì)以你最后一次的比例尺為標(biāo)準(zhǔn) 恢復(fù)到定位位置 我們需要的是 一開始的比例尺的定位
//拖線方法
- (IBAction)backAction {
//將用戶的位置 設(shè)置為地圖的中心點(diǎn)的經(jīng)緯度
self.mapView.centerCoordinate = self.mapView.userLocation.coordinate;
}
改善 發(fā)現(xiàn)問題 2.0給的不對(duì),確實(shí)用的是2.0的比例尺,但是和一開始的定位不對(duì),肯定就是span的錯(cuò)了,那么想法弄到最原始的span的兩個(gè)值就搞定
- (IBAction)backAction {
//將用戶的位置 設(shè)置為地圖的中心點(diǎn)的經(jīng)緯度
self.mapView.centerCoordinate = self.mapView.userLocation.coordinate;
//改善 確定區(qū)域:1.中心點(diǎn)經(jīng)緯度 2.經(jīng)緯度跨度
/*
region:
跨度結(jié)構(gòu)體(span):
typedef struct {
CLLocationDegrees latitudeDelta;
CLLocationDegrees longitudeDelta;
} MKCoordinateSpan;
//區(qū)域結(jié)構(gòu)體(region)
typedef struct {
CLLocationCoordinate2D center;
MKCoordinateSpan span;
} MKCoordinateRegion;
*/
//跨度 CLLocationDegrees latitudeDelta = double
MKCoordinateSpan span = MKCoordinateSpanMake(2.0, 2.0);
//給mapView的region賦值
self.mapView.region = MKCoordinateRegionMake(self.mapView.userLocation.location.coordinate, span);
}
求地圖定位的時(shí)候span兩個(gè)值的大小 想都不用想 肯定是代理方法
//地圖區(qū)域一旦改變 就會(huì)調(diào)用
-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
NSLog(@"longitudeDelta = %f,latitudeDelta = %f",self.mapView.region.span.longitudeDelta,self.mapView.region.span.latitudeDelta);
}
打印出來得出結(jié)果 longitudeDelta = 0.015407,latitudeDelta = 0.021045 這就是我們需要的兩個(gè)span值
修改span
MKCoordinateSpan span = MKCoordinateSpanMake(0.015407, 0.021045);
縮小地圖 span的兩個(gè)值就變大 放大就變小 比例尺嗎 越清晰表明比例越小
變化的時(shí)候讓其有動(dòng)畫效果--要用動(dòng)畫的時(shí)候,先看看有沒有系統(tǒng)自己的,系統(tǒng)的無非都是set帶動(dòng)畫,找一下,如果還沒有,自己加一個(gè)動(dòng)畫,我前面寫過UIView的那些動(dòng)畫,這里都可以用上.
[self.mapView setRegion:MKCoordinateRegionMake(self.mapView.userLocation.location.coordinate, span) animated:true];
增加放大縮小按鈕###
拖線方法
//改變地圖大小的按鈕
- (IBAction)changeMap:(UIButton *)sender {
switch (sender.tag) {
case 1:{//放大 地圖的跨度中的經(jīng)緯度 * 0.5
//修改當(dāng)前跨度
//緯度跨度
CGFloat latitudeDelta = self.mapView.region.span.latitudeDelta * 0.5;
//經(jīng)度跨度
CGFloat longitudeDelta = self.mapView.region.span.longitudeDelta * 0.5;
MKCoordinateSpan span = MKCoordinateSpanMake(latitudeDelta, longitudeDelta);
[self.mapView setRegion:MKCoordinateRegionMake(self.mapView.centerCoordinate, span) animated:true];
break;
}
case 2:{//縮小 地圖的跨度中的經(jīng)緯度 * 2
//修改當(dāng)前跨度
//緯度跨度
CGFloat latitudeDelta = self.mapView.region.span.latitudeDelta * 2;
//經(jīng)度跨度
CGFloat longitudeDelta = self.mapView.region.span.longitudeDelta * 2;
MKCoordinateSpan span = MKCoordinateSpanMake(latitudeDelta, longitudeDelta);
[self.mapView setRegion:MKCoordinateRegionMake(self.mapView.centerCoordinate, span) animated:true];
break;
}
default:
break;
}
}
這里重點(diǎn)有兩個(gè) 1,你要確定的中心點(diǎn)位置,千萬(wàn)不要寫成self.mapView.userLocation.coordinate這個(gè),你是以當(dāng)前用戶定位所在為中心點(diǎn),你看別處的時(shí)候相放大就會(huì)回到定位位置,所以你要清晰的知道 中心點(diǎn)是當(dāng)前地圖的中心self.mapView.centerCoordinate 2,還是放大縮小比例尺的邏輯問題,錯(cuò)了沒事,弄錯(cuò)只會(huì)相反嘛,改過就是了.
本文不針對(duì)任何學(xué)術(shù)性研究,單純?yōu)榱藠蕵?看不慣我就直說,反正我不改.
寶劍鋒從磨礪出,梅花香自苦寒來.