1、导入库
CoreLocation、
MapKit
2、在AppDelegate开启定位功能
- (
void
)startLocation
{
//1. 定位管理器
_locationManager = [[ CLLocationManager alloc ] init ];
if (![ CLLocationManager locationServicesEnabled ]) {
MyLog ( @" 定位服务当前可能尚未打开 , 请设置打开 !" );
return ;
}
//2. 如果没有授权则请求用户授权
if ([ CLLocationManager authorizationStatus ] == kCLAuthorizationStatusNotDetermined ) {
[ _locationManager requestWhenInUseAuthorization ];
} else if ([ CLLocationManager authorizationStatus ] == kCLAuthorizationStatusAuthorizedWhenInUse ) {
//2.1 设置代理
_locationManager . delegate = self ;
//2.2 设置定位精度
_locationManager . desiredAccuracy = kCLLocationAccuracyBest ;
//2.3 设置定位频率
_locationManager . distanceFilter = 100.0 ;
//2.4 开启定位功能
[ _locationManager startUpdatingLocation ];
}
}
#pragma mark CoreLocation 代理
- ( void )locationManager:( CLLocationManager *)manager didUpdateLocations:( NSArray < CLLocation *> *)locations
{
CLLocation *location = [locations firstObject ];
CLLocationCoordinate2D coordinate = location. coordinate ;
{
//1. 定位管理器
_locationManager = [[ CLLocationManager alloc ] init ];
if (![ CLLocationManager locationServicesEnabled ]) {
MyLog ( @" 定位服务当前可能尚未打开 , 请设置打开 !" );
return ;
}
//2. 如果没有授权则请求用户授权
if ([ CLLocationManager authorizationStatus ] == kCLAuthorizationStatusNotDetermined ) {
[ _locationManager requestWhenInUseAuthorization ];
} else if ([ CLLocationManager authorizationStatus ] == kCLAuthorizationStatusAuthorizedWhenInUse ) {
//2.1 设置代理
_locationManager . delegate = self ;
//2.2 设置定位精度
_locationManager . desiredAccuracy = kCLLocationAccuracyBest ;
//2.3 设置定位频率
_locationManager . distanceFilter = 100.0 ;
//2.4 开启定位功能
[ _locationManager startUpdatingLocation ];
}
}
#pragma mark CoreLocation 代理
- ( void )locationManager:( CLLocationManager *)manager didUpdateLocations:( NSArray < CLLocation *> *)locations
{
CLLocation *location = [locations firstObject ];
CLLocationCoordinate2D coordinate = location. coordinate ;
MyLog(@"
经度:
%f,
纬度:
%f,
海拔:
%f,
航向:
%f,
行走速度:
%f",coordinate.longitude,coordinate.latitude,location.altitude,location.course,location.speed);
}
3、坐标转地址
- (
void
)getAddressByLatitude:(
CLLocationDegrees
)latitude longitude:(
CLLocationDegrees
)longitude
{
// 反地理编码
CLLocation *location = [[ CLLocation alloc ] initWithLatitude :latitude longitude :longitude];
CLGeocoder *geoCode = [[ CLGeocoder alloc ] init ];
[geoCode reverseGeocodeLocation :location completionHandler :^( NSArray < CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
CLPlacemark *placeMark = [placemarks firstObject ];
NSDictionary *addressDict = placeMark. addressDictionary ;
NSString *name = [addressDict objectForKey : @"Name" ];
MyLog ( @" 详细信息 :%@" ,name);
_addressTextField . text = name;
}];
{
// 反地理编码
CLLocation *location = [[ CLLocation alloc ] initWithLatitude :latitude longitude :longitude];
CLGeocoder *geoCode = [[ CLGeocoder alloc ] init ];
[geoCode reverseGeocodeLocation :location completionHandler :^( NSArray < CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
CLPlacemark *placeMark = [placemarks firstObject ];
NSDictionary *addressDict = placeMark. addressDictionary ;
NSString *name = [addressDict objectForKey : @"Name" ];
MyLog ( @" 详细信息 :%@" ,name);
_addressTextField . text = name;
}];
}
4、调用地图功能
#pragma mark
添加地图控件
- ( void )addMapKit
{
//1. 初始化及设置代理
_mapView = [[ MKMapView alloc ] initWithFrame : self . view . bounds ];
_mapView . autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight ;
_mapView . showsUserLocation = YES ;
_mapView . delegate = self ;
[ self . view addSubview : _mapView ];
//2. 请求定位服务
_locationManager = [[ CLLocationManager alloc ] init ];
if (![ CLLocationManager locationServicesEnabled ]||[ CLLocationManager authorizationStatus ]!= kCLAuthorizationStatusAuthorizedWhenInUse ){
[ _locationManager requestWhenInUseAuthorization ];
}
//3. 用户位置跟踪
_mapView . userTrackingMode = MKUserTrackingModeFollow ;
//4. 设置地图类型
_mapView . mapType = MKMapTypeStandard ;
//5. 添加大头针
- ( void )addMapKit
{
//1. 初始化及设置代理
_mapView = [[ MKMapView alloc ] initWithFrame : self . view . bounds ];
_mapView . autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight ;
_mapView . showsUserLocation = YES ;
_mapView . delegate = self ;
[ self . view addSubview : _mapView ];
//2. 请求定位服务
_locationManager = [[ CLLocationManager alloc ] init ];
if (![ CLLocationManager locationServicesEnabled ]||[ CLLocationManager authorizationStatus ]!= kCLAuthorizationStatusAuthorizedWhenInUse ){
[ _locationManager requestWhenInUseAuthorization ];
}
//3. 用户位置跟踪
_mapView . userTrackingMode = MKUserTrackingModeFollow ;
//4. 设置地图类型
_mapView . mapType = MKMapTypeStandard ;
//5. 添加大头针
[self addAnnotation];
}
#pragma mark
添加大头针
- ( void )addAnnotation
{
CLLocationCoordinate2D shopCoordinate = CLLocationCoordinate2DMake ( _latitude , _longitude );
MKPointAnnotation *annotation = [[ MKPointAnnotation alloc ] init ];
annotation. coordinate = shopCoordinate;
annotation. title = _name ;
[ _mapView addAnnotation :annotation];
}
#pragma mark - MapView 代理
- ( void )mapView:( MKMapView *)mapView didUpdateUserLocation:( MKUserLocation *)userLocation
{
if ( _mapView ) return ;
//1. 位置
CLLocationCoordinate2D center = userLocation. location . coordinate ;
//2. 跨度
MKCoordinateRegion region = MKCoordinateRegionMake (center, kSpan );
//3. 区域
[mapView setRegion :region animated : YES ];
_mapView = mapView;
}
#pragma mark 自定义大头针图片
- ( MKAnnotationView *)mapView:( MKMapView *)mapView viewForAnnotation:( id < MKAnnotation >)annotation
{
if ([annotation isKindOfClass :[ MKPointAnnotation class]]){
MKPinAnnotationView *newAnnotationView = [[ MKPinAnnotationView alloc ] initWithAnnotation :annotation reuseIdentifier : @"myAnnotation" ];
newAnnotationView. pinColor = MKPinAnnotationColorPurple ;
// 设置该标注点动画显示
newAnnotationView. animatesDrop = YES ;
newAnnotationView. annotation = annotation;
// 把大头针换成别的图片
newAnnotationView. image = [ UIImage imageNamed : @"my_mapLocate_annotation.png" ];
return newAnnotationView;
}
return nil ;
- ( void )addAnnotation
{
CLLocationCoordinate2D shopCoordinate = CLLocationCoordinate2DMake ( _latitude , _longitude );
MKPointAnnotation *annotation = [[ MKPointAnnotation alloc ] init ];
annotation. coordinate = shopCoordinate;
annotation. title = _name ;
[ _mapView addAnnotation :annotation];
}
#pragma mark - MapView 代理
- ( void )mapView:( MKMapView *)mapView didUpdateUserLocation:( MKUserLocation *)userLocation
{
if ( _mapView ) return ;
//1. 位置
CLLocationCoordinate2D center = userLocation. location . coordinate ;
//2. 跨度
MKCoordinateRegion region = MKCoordinateRegionMake (center, kSpan );
//3. 区域
[mapView setRegion :region animated : YES ];
_mapView = mapView;
}
#pragma mark 自定义大头针图片
- ( MKAnnotationView *)mapView:( MKMapView *)mapView viewForAnnotation:( id < MKAnnotation >)annotation
{
if ([annotation isKindOfClass :[ MKPointAnnotation class]]){
MKPinAnnotationView *newAnnotationView = [[ MKPinAnnotationView alloc ] initWithAnnotation :annotation reuseIdentifier : @"myAnnotation" ];
newAnnotationView. pinColor = MKPinAnnotationColorPurple ;
// 设置该标注点动画显示
newAnnotationView. animatesDrop = YES ;
newAnnotationView. annotation = annotation;
// 把大头针换成别的图片
newAnnotationView. image = [ UIImage imageNamed : @"my_mapLocate_annotation.png" ];
return newAnnotationView;
}
return nil ;
}