
正文
iOS- 用MapKit和CoreLocation 来实现移动设备(地图与定位)
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
1.前言
发现在很多的社交软件都引入了地图和定位功能,如果我们要想实现这两大功能,需要利用到两个框架:MapKit和CoreLocation
我们先来看看CoreLocation框架:
它可以使用硬件设备来进行定位服务,不需要地图,精度相对略差,省电。
而MapKit框架:
能够使应用程序做一些地图展示与交互的相关功能,必须有地图,精度相对较高,费电。
下面我就说说它的能实现的一些常用功能
2.地图常见操作
@property (weak, nonatomic) IBOutletMKMapView *mapView;
2.1. 标记用户当前位置、跟踪用户位置
// 标记用户当前位置
// 跟踪用户位置
[_mapView setUserTrackingMode:MKUserTrackingModeFollow];
2.1. StartFragment地图的类型 EndFragment
可以通过设置MKMapView的mapViewType设置地图类型
MKMapTypeStandard 普通地图
MKMapTypeSatellite 卫星云图
StartFragmentMKMapTypeHybrid普通地图覆盖于卫星云图之上
// 地图类型
[_mapView setMapType:MKMapTypeHybrid];
2.2.通过代理的方式可以跟踪用户的位置变化
2.2.1.mapViewWillStartLoadingMap: 当地图界面将要加载时调用
2.2.2.mapView:viewForAnnotation: 当地图上有大头针时调用
2.2.3.mapViewWillStartLocatingUser:当准备进行一个位置定位时调用
2.2.4.mapView:regionDidChangeAnimated: 当显示的区域发生变化时调用
2.2.5.mapView:didUpdateUserLocation:当用户位置发生变化时调用
// 通过代理的方式可以跟踪用户的位置变化
_mapView.delegate = self;
#pragma mark - 地图代理方法
#pragma mark 会频繁调用,非常费电
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
// 显示用户周边信息 拉近地图 StartFragment设置地图显示区域
CLLocationCoordinate2D center = userLocation.location.coordinate;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(center, 100.0, 100.0);
[mapView setRegion:region animated:YES];
}
StartFragment2.3.添加默认大头针(地标)
1.通过MapView的addAnnotation方法可以添加一个大头针到地图上
2.通过MapView的addAnnotations方法可以添加多个大头针到地图上
头文件里说明:- (void)addAnnotation:(id <MKAnnotation>)annotation;
4.说明需要传入一个遵守了MKAnnotation协议的对象
MyAnnotation *annotation2 = [[MyAnnotation alloc] init];
annotation2.coordinate = CLLocationCoordinate2DMake(, );
annotation2.title = @"重庆";
annotation2.subtitle = @"重庆详细描述";
annotation2.imageName = @"head0";
[_mapView addAnnotation:annotation2];
2.4.添加自定义大头针(重用地标)
#pragma mark 自定义大头针视图
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
// 判断annotation参数是否是MyAnnotation
// 如果不是MyAnnotaion说明是系统的大头针,无需做处理
if (![annotation isKindOfClass:[MyAnnotation class]]) {
// 使用系统默认的大头针
return nil;
}
// 可重用标示符
static NSString *ID = @"MyAnno";
// 查询可重用的大头针
MKAnnotationView *annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:ID];
// 如果没有找到,再去实例化大头针
if (annoView == nil) {
annoView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ID];
// 自定义大头针视图,如果要接受用户响应,需要设置此属性
annoView.canShowCallout = YES;
}
// 设置大头针
annoView.annotation = annotation;
// 转换成MyAnnotation
// 设置大头针视图的图像
MyAnnotation *anno = annotation;
annoView.image = [UIImage imageNamed:anno.imageName];
NSLog(@"自定义大头针");
return annoView;
}
3.定位
// 定位服务管理器
CLLocationManager *_locationManager;
// 使用地理编码器
CLGeocoder *_geocoder;
1. 在开发LBS类的应用时,获取用户定位信息之前,一定要判断一下定位服务是否允许
locationServicesEnabled
if (![CLLocationManager locationServicesEnabled]) {
NSLog(@"定位服务不可用!");
return;
}
2. 开启定位,获取自己的当前位置
[_locationManager startUpdatingLocation];
3. 根据经纬度,知道准确的地名
reverseGeocodeLocation
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
NSLog(@"位置变化: %@", locations[]);
// 根据经纬度查找(去苹果后台查找准确的位置,必须联网才能用)
[_geocoder reverseGeocodeLocation:locations[] completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"%@", placemarks[]);
}];
}
4. 根据定名,获取到经纬度,一般用在导航
geocodeAddressString
[_geocoder geocodeAddressString:@"王府井" completionHandler:^(NSArray *placemarks, NSError *error) {
for (CLPlacemark *placemark in placemarks) {
NSLog(@"aaaa______%@ %lu", placemark, (unsigned long)placemarks.count);
}
}];
我们先来看看CoreLocation框架:
它可以使用硬件设备来进行定位服务,不需要地图,精度相对略差,省电。
而MapKit框架:
能够使应用程序做一些地图展示与交互的相关功能,必须有地图,精度相对较高,费电。
下面我就说说它的能实现的一些常用功能
2.地图常见操作
@property (weak, nonatomic) IBOutletMKMapView *mapView;
2.1. 标记用户当前位置、跟踪用户位置
// 标记用户当前位置
// 跟踪用户位置
[_mapView setUserTrackingMode:MKUserTrackingModeFollow];
2.1. StartFragment地图的类型 EndFragment
可以通过设置MKMapView的mapViewType设置地图类型
MKMapTypeStandard 普通地图
MKMapTypeSatellite 卫星云图
StartFragmentMKMapTypeHybrid普通地图覆盖于卫星云图之上
// 地图类型
[_mapView setMapType:MKMapTypeHybrid];
2.2.通过代理的方式可以跟踪用户的位置变化
2.2.1.mapViewWillStartLoadingMap: 当地图界面将要加载时调用
2.2.2.mapView:viewForAnnotation: 当地图上有大头针时调用
2.2.3.mapViewWillStartLocatingUser:当准备进行一个位置定位时调用
2.2.4.mapView:regionDidChangeAnimated: 当显示的区域发生变化时调用
// 标记用户当前位置
// 跟踪用户位置
[_mapView setUserTrackingMode:MKUserTrackingModeFollow];
可以通过设置MKMapView的mapViewType设置地图类型
MKMapTypeStandard 普通地图
MKMapTypeSatellite 卫星云图
StartFragmentMKMapTypeHybrid普通地图覆盖于卫星云图之上
// 地图类型
[_mapView setMapType:MKMapTypeHybrid];
2.2.通过代理的方式可以跟踪用户的位置变化
2.2.1.mapViewWillStartLoadingMap: 当地图界面将要加载时调用
2.2.2.mapView:viewForAnnotation: 当地图上有大头针时调用
2.2.3.mapViewWillStartLocatingUser:当准备进行一个位置定位时调用
2.2.4.mapView:regionDidChangeAnimated: 当显示的区域发生变化时调用
2.2.5.mapView:didUpdateUserLocation:当用户位置发生变化时调用
// 通过代理的方式可以跟踪用户的位置变化
_mapView.delegate = self;
#pragma mark - 地图代理方法
#pragma mark 会频繁调用,非常费电
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{ // 显示用户周边信息 拉近地图 StartFragment设置地图显示区域
CLLocationCoordinate2D center = userLocation.location.coordinate;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(center, 100.0, 100.0);
[mapView setRegion:region animated:YES];
}
StartFragment2.3.添加默认大头针(地标)
1.通过MapView的addAnnotation方法可以添加一个大头针到地图上
2.通过MapView的addAnnotations方法可以添加多个大头针到地图上
头文件里说明:- (void)addAnnotation:(id <MKAnnotation>)annotation;
4.说明需要传入一个遵守了MKAnnotation协议的对象
MyAnnotation *annotation2 = [[MyAnnotation alloc] init];
annotation2.coordinate = CLLocationCoordinate2DMake(, );
annotation2.title = @"重庆";
annotation2.subtitle = @"重庆详细描述";
annotation2.imageName = @"head0";
[_mapView addAnnotation:annotation2];
2.4.添加自定义大头针(重用地标)
#pragma mark 自定义大头针视图
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
// 判断annotation参数是否是MyAnnotation
// 如果不是MyAnnotaion说明是系统的大头针,无需做处理
if (![annotation isKindOfClass:[MyAnnotation class]]) {
// 使用系统默认的大头针
return nil;
}
// 可重用标示符
static NSString *ID = @"MyAnno";
// 查询可重用的大头针
MKAnnotationView *annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:ID];
// 如果没有找到,再去实例化大头针
if (annoView == nil) {
annoView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ID];
// 自定义大头针视图,如果要接受用户响应,需要设置此属性
annoView.canShowCallout = YES;
}
// 设置大头针
annoView.annotation = annotation;
// 转换成MyAnnotation
// 设置大头针视图的图像
MyAnnotation *anno = annotation;
annoView.image = [UIImage imageNamed:anno.imageName];
NSLog(@"自定义大头针");
return annoView;
}
3.定位
// 定位服务管理器
CLLocationManager *_locationManager;
// 使用地理编码器
CLGeocoder *_geocoder;
1. 在开发LBS类的应用时,获取用户定位信息之前,一定要判断一下定位服务是否允许
MyAnnotation *annotation2 = [[MyAnnotation alloc] init];
annotation2.coordinate = CLLocationCoordinate2DMake(, );
annotation2.title = @"重庆";
annotation2.subtitle = @"重庆详细描述";
annotation2.imageName = @"head0";
[_mapView addAnnotation:annotation2];
#pragma mark 自定义大头针视图
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
// 判断annotation参数是否是MyAnnotation
// 如果不是MyAnnotaion说明是系统的大头针,无需做处理
if (![annotation isKindOfClass:[MyAnnotation class]]) {
// 使用系统默认的大头针
return nil;
} // 可重用标示符
static NSString *ID = @"MyAnno"; // 查询可重用的大头针
MKAnnotationView *annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:ID]; // 如果没有找到,再去实例化大头针
if (annoView == nil) {
annoView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ID]; // 自定义大头针视图,如果要接受用户响应,需要设置此属性
annoView.canShowCallout = YES;
} // 设置大头针
annoView.annotation = annotation;
// 转换成MyAnnotation
// 设置大头针视图的图像
MyAnnotation *anno = annotation;
annoView.image = [UIImage imageNamed:anno.imageName]; NSLog(@"自定义大头针"); return annoView;
}
3.定位
// 定位服务管理器
CLLocationManager *_locationManager;
// 使用地理编码器
CLGeocoder *_geocoder;
1. 在开发LBS类的应用时,获取用户定位信息之前,一定要判断一下定位服务是否允许
// 定位服务管理器
CLLocationManager *_locationManager; // 使用地理编码器
CLGeocoder *_geocoder;
locationServicesEnabled
if (![CLLocationManager locationServicesEnabled]) {
NSLog(@"定位服务不可用!");
return;
}
2. 开启定位,获取自己的当前位置
[_locationManager startUpdatingLocation];
3. 根据经纬度,知道准确的地名
reverseGeocodeLocation
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
NSLog(@"位置变化: %@", locations[]); // 根据经纬度查找(去苹果后台查找准确的位置,必须联网才能用)
[_geocoder reverseGeocodeLocation:locations[] completionHandler:^(NSArray *placemarks, NSError *error) { NSLog(@"%@", placemarks[]); }];
}
4. 根据定名,获取到经纬度,一般用在导航
geocodeAddressString
[_geocoder geocodeAddressString:@"王府井" completionHandler:^(NSArray *placemarks, NSError *error) {
for (CLPlacemark *placemark in placemarks) {
NSLog(@"aaaa______%@ %lu", placemark, (unsigned long)placemarks.count);
}
}];
清澈Saup






