导入框架 CoreLocation和 MapKit,并引入头文件
// 首先需要使用定位系统
1.创建管理者对象
2.判断系统服务是否开启
3.判断当前定位的授权状态
// 1.
创建管理者对象 设置为全局变量
manager = [[ CLLocationManager alloc ] init ];
// 2. 判断系统定位服务是否开启
if (![ CLLocationManager locationServicesEnabled ])
{
NSLog ( @" 系统定位服务未开启 " );
return ;
}
// 3. 判断当前定位的授权状态
if ([ CLLocationManager authorizationStatus ] == 0 )
{
// 判断当前 iOS 系统是否为 8.0 以上
if ([[[ UIDevice currentDevice ] systemVersion ] floatValue ] >= 8.0 )
{
// 做授权 注意:一定要修改 plist 文件做配置 NSLocationWhenInUseUsageDescription
[ manager requestWhenInUseAuthorization ];
}
manager = [[ CLLocationManager alloc ] init ];
// 2. 判断系统定位服务是否开启
if (![ CLLocationManager locationServicesEnabled ])
{
NSLog ( @" 系统定位服务未开启 " );
return ;
}
// 3. 判断当前定位的授权状态
if ([ CLLocationManager authorizationStatus ] == 0 )
{
// 判断当前 iOS 系统是否为 8.0 以上
if ([[[ UIDevice currentDevice ] systemVersion ] floatValue ] >= 8.0 )
{
// 做授权 注意:一定要修改 plist 文件做配置 NSLocationWhenInUseUsageDescription
[ manager requestWhenInUseAuthorization ];
}
}
// ---------------------
地图
MKM
apView
的使用
-------------------------
- 初始化地图对象
_mapView
= [[
MKMapView
alloc
]
initWithFrame
:
self
.
view
.
bounds
];
- 设置地图属性
2.1 设置用户跟踪模式
userTrackingMode
_mapView
.
userTrackingMode
=
MKUserTrackingModeFollow
;
2.2 设置地图类型 mapType
_mapView
.
mapType
=
MKMapTypeStandard
;
//一般使用
标准地图
2.3 设置代理
_mapView
.
delegate
=
self
;
- 添加地图到当前视图
[
self
.
view
addSubview
:
_mapView
];
//
更新用户位置,只要用户改变则调用此方法
// 代理方法设置地图当前显示的范围
- (
void
)mapView:(
MKMapView
*)mapView didUpdateUserLocation:(
MKUserLocation
*)userLocation
{
// 用户位置刷新
{
// 用户位置刷新
NSLog
(
@"
用户位置刷新
"
);
//
设置地图显示的区域范围
(
如果不进行区域设置会自动显示区域范围并制定当前用户位置为地图中心店
)
//
初始化跨度变量
MKCoordinateSpan
span =
MKCoordinateSpanMake
(
1
,
1
);
//
创建显示范围对象
MKCoordinateRegion
region =
MKCoordinateRegionMake
(userLocation.
location
.
coordinate
, span);
//
设置显示范围
[mapView
setRegion
:region];
}
- ( void )mapViewDidFinishLoadingMap:( MKMapView *)mapView
{
NSLog ( @" 地图加载完毕 " );
- ( void )mapViewDidFinishLoadingMap:( MKMapView *)mapView
{
NSLog ( @" 地图加载完毕 " );
}
// -------------------------- 位置跟踪 ------------------------------
UIButton
*button = [[
UIButton
alloc
]
initWithFrame
:
CGRectMake
(
20
,
40
,
50
,
30
)];
button. backgroundColor = [ UIColor cyanColor ];
[ self . view addSubview :button];
button. backgroundColor = [ UIColor cyanColor ];
[ self . view addSubview :button];
[button addTarget:self action:@selector(followLocation) forControlEvents:UIControlEventTouchUpInside];
//
位置跟踪
- ( void )followLocation
- ( void )followLocation
{
//
设置屏幕中心位置
[
_mapView
setCenterCoordinate
:
_mapView
.
userLocation
.
coordinate
animated
:
YES
];
}
// --------------------------- 点击添加大头针 -----------------------------
// 点击地图添加大头针
//
创建点击手势
UITapGestureRecognizer
*tap = [[
UITapGestureRecognizer
alloc
]
initWithTarget
:
self
action
:
@selector
(createAnnotation:)];
//
将手势添加到地图上
[_mapView addGestureRecognizer:tap];
//
添加大头针
// 大头针对象需要通过NSObject的子类实现MKAnnotation协议,重写协议中的coordinate(标记位置)、title(标题)、subtitle(子标题)三个属性来创建
- (
void
)createAnnotation:(
UIGestureRecognizer
*)tap
{
// 1. 获取点击的点
{
// 1. 获取点击的点
CGPoint
point = [tap
locationInView
:
_mapView
];
// 2
,将点击点的坐标转换为经纬度坐标 converPoint:toCoordinateFromView
CLLocationCoordinate2D
coordinate = [
_mapView
convertPoint
:point
toCoordinateFromView
:
_mapView
];
// 3.
创建大头针对象,并且设置坐标
BVAnnotation *annotation = [[ BVAnnotation alloc ] init ];
// 设置坐标 以及属性
annotation. coordinate = coordinate;
annotation. title = @"Island" ;
annotation. subtitle = @"hello world" ;
annotation. image = [ UIImage imageNamed : @"category_3" ];
// 4. 将大头针对象添加到 mapView 上
BVAnnotation *annotation = [[ BVAnnotation alloc ] init ];
// 设置坐标 以及属性
annotation. coordinate = coordinate;
annotation. title = @"Island" ;
annotation. subtitle = @"hello world" ;
annotation. image = [ UIImage imageNamed : @"category_3" ];
// 4. 将大头针对象添加到 mapView 上
[_mapView addAnnotation:annotation];
}
//
自定义大头针样式
- (
MKAnnotationView
*)mapView:(
MKMapView
*)mapView viewForAnnotation:(
id
<
MKAnnotation
>)annotation
{
// 由于当前位置的标注也是一个大头针,所以此时需要判断,此代理方法返回nil使用默认大头针视图
//
需要判断当前大头针对象是否为用户位置大头针
如果不是则创建自定义视图
if ([annotation isKindOfClass :[ BVAnnotation class ]])
{
// 大头针视图的创建类似于 UITableViewCell 会执行复用
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier : @"annotationView" ];
// 判断 annotationView 是否存在 复用池中不存在则新建
if (!annotationView)
{
annotationView = [[ MKAnnotationView alloc ] initWithAnnotation :annotation reuseIdentifier : @"annotationView" ];
// 设置选中时显示详情框 允许交互点击
annotationView. canShowCallout = YES ;
// 给 annotationView详情框 添加左侧的辅助视图
annotationView. leftCalloutAccessoryView = [ UIButton buttonWithType : UIButtonTypeContactAdd ];
if ([annotation isKindOfClass :[ BVAnnotation class ]])
{
// 大头针视图的创建类似于 UITableViewCell 会执行复用
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier : @"annotationView" ];
// 判断 annotationView 是否存在 复用池中不存在则新建
if (!annotationView)
{
annotationView = [[ MKAnnotationView alloc ] initWithAnnotation :annotation reuseIdentifier : @"annotationView" ];
// 设置选中时显示详情框 允许交互点击
annotationView. canShowCallout = YES ;
// 给 annotationView详情框 添加左侧的辅助视图
annotationView. leftCalloutAccessoryView = [ UIButton buttonWithType : UIButtonTypeContactAdd ];
}
//
重新设置大头针对象(因为有可能是从复用池中取出的,位置是放到复用池时的位置)
annotationView.annotation
= annotation;
//
设置
annotationView
的背景图片
annotationView.image
= ((
BVAnnotation
*)annotation).
image
;
return
annotationView;
}
return
nil
;
}
//
设置添加大头针时从天而降的效果
- ( MKAnnotationView *)mapView:( MKMapView *)mapView viewForAnnotation:( id < MKAnnotation >)annotation
{
// 判断是否为用户当前位置大头针 如果是则跳过
if ([annotation isKindOfClass :[ BVAnnotation class ]])
{
// 创建带特效的大头针视图
MKPinAnnotationView *pinAnnView = ( MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier : @"pin" ];
if (!pinAnnView)
{
pinAnnView = [[ MKPinAnnotationView alloc ] initWithAnnotation :annotation reuseIdentifier : @"pin" ];
// 设置点击时显示详情视图
- ( MKAnnotationView *)mapView:( MKMapView *)mapView viewForAnnotation:( id < MKAnnotation >)annotation
{
// 判断是否为用户当前位置大头针 如果是则跳过
if ([annotation isKindOfClass :[ BVAnnotation class ]])
{
// 创建带特效的大头针视图
MKPinAnnotationView *pinAnnView = ( MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier : @"pin" ];
if (!pinAnnView)
{
pinAnnView = [[ MKPinAnnotationView alloc ] initWithAnnotation :annotation reuseIdentifier : @"pin" ];
// 设置点击时显示详情视图
pinAnnView.canShowCallout
=
YES
;
//
设置从天而降效果
pinAnnView.
animatesDrop
=
YES
;
//
修改系统大头针颜色
pinAnnView.pinColor
=
MKPinAnnotationColorPurple
;
//
设置辅助视图
pinAnnView. leftCalloutAccessoryView = [ UIButton buttonWithType : UIButtonTypeContactAdd ];
pinAnnView. leftCalloutAccessoryView = [ UIButton buttonWithType : UIButtonTypeContactAdd ];
}
//
重新设置
annotation
对象
pinAnnView.annotation
= annotation;
//
在
pinAnnView
添加图片
UIImageView
*imageView = [[
UIImageView
alloc
]
initWithFrame
:pinAnnView.
bounds
];
//
设置图片
imageView.image
= [(
BVAnnotation
*)annotation
image
];
//
将图片添加到大头针视图上
[pinAnnView addSubview :imageView];
[pinAnnView addSubview :imageView];
return
pinAnnView;
}
return nil ;
return nil ;
}