MapKit基本應(yīng)用
//創(chuàng)建地圖
_mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:_mapView];
//設(shè)置地圖類型
//Standard 標(biāo)準(zhǔn)地圖
//Satellite 衛(wèi)星地圖
//Hybrid 混合模式
_mapView.mapType = MKMapTypeStandard;
//將地圖定位到指定的位置
//參數(shù)1:MKCoordinateRegion想要鎖定的位置信息(CLLocationCoordinate2D經(jīng)緯度,MKCoordinateSpan放大系數(shù))
[_mapView setRegion:MKCoordinateRegionMake(CLLocationCoordinate2DMake(30.67, 104.06), MKCoordinateSpanMake(0.01, 0.01)) animated:YES];
//是否可以縮放
_mapView.zoomEnabled = true;
//是否可以滾動(dòng)
_mapView.scrollEnabled = true;
//是否可以旋轉(zhuǎn)
_mapView.rotateEnabled = true;
//是否顯示指南針
_mapView.showsCompass = true;
//是否顯示建筑
_mapView.showsBuildings = true;
//是否顯示交通
_mapView.showsTraffic = true;
//是否顯示比例尺
_mapView.showsScale = true;
//不跟蹤用戶的位置
_mapView.userTrackingMode = MKUserTrackingModeNone;
//跟蹤用戶位置并在地圖上顯示
_mapView.userTrackingMode = MKUserTrackingModeFollow;
//跟蹤用戶的位置并根據(jù)用戶前進(jìn)的方向進(jìn)行旋轉(zhuǎn)
_mapView.userTrackingMode = MKUserTrackingModeFollowWithHeading;
//設(shè)置代理
_mapView.delegate = self;
//創(chuàng)建大頭針大頭針
MKPointAnnotation * annotation = [[MKPointAnnotation alloc] init];
//設(shè)置大頭針位置
annotation.coordinate = CLLocationCoordinate2DMake(30.67, 104.06);
//設(shè)置大頭針的標(biāo)題
annotation.title = @"成都";
annotation.subtitle = @"雙流";
[_mapView addAnnotation:annotation];
//創(chuàng)建CLGeocoder地理編碼對(duì)象
CLGeocoder * geocoder = [[CLGeocoder alloc] init];
//根據(jù)輸入地址進(jìn)行反編碼
[geocoder geocodeAddressString:@"四川成都青羊區(qū)" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
//拿到編碼的地址
_mark1 = [placemarks firstObject];
_label.text = [NSString stringWithFormat:@"%f", _mark1.location.coordinate.latitude];
}];
[geocoder geocodeAddressString:@"四川成都雙流縣" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
_mark2 = [placemarks firstObject];
}];
//調(diào)用系統(tǒng)的導(dǎo)航
[self beginNavWithBeginPlacemark:_mark1 andEndPlacemark:_mark2];
- (void)beginNavWithBeginPlacemark:(CLPlacemark *)beginPlacemark andEndPlacemark:(CLPlacemark *)endPlacemark{
// 創(chuàng)建起點(diǎn):根據(jù) CLPlacemark 地標(biāo)對(duì)象創(chuàng)建 MKPlacemark 地標(biāo)對(duì)象
MKPlacemark *itemP1 = [[MKPlacemark alloc] initWithPlacemark:beginPlacemark];
MKMapItem *item1 = [[MKMapItem alloc] initWithPlacemark:itemP1];
// 創(chuàng)建終點(diǎn):根據(jù) CLPlacemark 地標(biāo)對(duì)象創(chuàng)建 MKPlacemark 地標(biāo)對(duì)象
MKPlacemark *itemP2 = [[MKPlacemark alloc] initWithPlacemark:endPlacemark];
MKMapItem *item2 = [[MKMapItem alloc] initWithPlacemark:itemP2];
NSDictionary *launchDic = @{
// 設(shè)置導(dǎo)航模式參數(shù)
MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving,
// 設(shè)置地圖類型
MKLaunchOptionsMapTypeKey : @(MKMapTypeHybrid),
// 設(shè)置是否顯示交通
MKLaunchOptionsShowsTrafficKey : @(YES),
};
// 根據(jù) MKMapItem 數(shù)組 和 啟動(dòng)參數(shù)字典 來(lái)調(diào)用系統(tǒng)地圖進(jìn)行導(dǎo)航
[MKMapItem openMapsWithItems:@[item1, item2] launchOptions:launchDic];
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者