- 设置 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];
});
}];