ios 自帶地圖簡單使用
阿新 • • 發佈:2019-01-27
- 設定 plist 配置檔案
- 匯入兩個地圖應用框架
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
- 定義地圖和地位屬性
@property (nonatomic , strong) MKMapView *mapView; ///<地圖
@property (nonatomic , strong) CLLocationManager *locManager; ///<獲得定位
- 重寫兩個屬性的 getter 方法
///重寫 getter - (CLLocationManager *)locManager{ if (!_locManager) { _locManager = [[CLLocationManager alloc]init]; _locManager.activityType = CLActivityTypeFitness; ///<步行導航 _locManager.delegate = self; } return _locManager; } - (MKMapView *)mapView{ if (!_mapView) { self.mapView = [[MKMapView alloc]initWithFrame:self.view.frame]; self.mapView.delegate = self; //設定地圖型別 _mapView.mapType = MKMapTypeStandard; //平面地圖 } return _mapView; }
- 在viewDidLoad 中設定使用者授權
//申請使用者授權
[self.locManager requestWhenInUseAuthorization];
- 遵守協議
<CLLocationManagerDelegate ,MKMapViewDelegate>
- 得到當前位置觸發事件
[_locManager startUpdatingLocation]; //開始更新當前位置資訊
-
CLLocationManagerDelegate代理方法中獲取當前位置
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{ //得到當前位置 CLLocation *currentLocation = locations.lastObject; //位置 ,此物件已經採用了 MK 協議 MKPointAnnotation *point = [[MKPointAnnotation alloc]init]; point.coordinate = currentLocation.coordinate; point.title = @"當前位置"; //地址解析 CLGeocoder *gecoder = [[CLGeocoder alloc]init]; [gecoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { CLPlacemark *place = placemarks.lastObject; point.subtitle = place.name; }]; //新增大頭針 [_mapView addAnnotation:point]; //需要將地圖的顯示區域變小 MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(currentLocation.coordinate, 800, 800); [_mapView setRegion:region animated:YES]; }
-
MKMapViewDelegate代理設定描邊
//設定錨點樣式 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{ MKPinAnnotationView *pin = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"pin"]; if (!pin) { pin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"pin"]; } pin.pinTintColor = [MKPinAnnotationView purplePinColor]; pin.animatesDrop = YES; pin.canShowCallout = YES; return pin; }
- 定位自定義的位置(您輸入的位置) 點選事件
//傳遞過來省會的字串做地址解析
CLGeocoder *geocoder = [[CLGeocoder alloc]init];
[geocoder geocodeAddressString:@"河北省邯鄲市永年區" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
CLPlacemark *place = placemarks.lastObject;
CLLocationCoordinate2D coord = place.location.coordinate;
dispatch_async(dispatch_get_main_queue(), ^{
//位置 ,此物件已經採用了 MK 協議
MKPointAnnotation *point = [[MKPointAnnotation alloc]init];
point.coordinate = coord;
//新增大頭針
[self.mapView addAnnotation:point];
point.title = @"當前位置";
[geocoder reverseGeocodeLocation:place.location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
CLPlacemark *place = placemarks.lastObject;
point.subtitle = place.name;
}];
//需要將地圖的顯示區域變小
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 800, 800);
[self.mapView setRegion:region animated:YES];
});
}];