社会分享、推送、地图类iOS技术iOS 开发每天分享优质文章

MapKit的基本使用

2016-07-02  本文已影响1677人  IIronMan

题外话:先学习CoreLocation的原因是:MapKit有一部分知识是基于CoreLocation,所以前面必须先了解CoreLocation

注意事项:

地图显示的概括:(导入框架<MapKit/MapKit.h>)

跟踪类型 mapView的代理方法
   -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
   {

     6.1 利用反地理编码,利用用户的经纬度来获取用户的位置名字,首先要在外面建立一个CLGeocoder *geocoder对象   
    
     6.2 调用一个方法(如下图):userLocation是大头针模型
   
         第一个参数直接是用户的位置:userLocation.location

        [self.geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
    
           CLPlacemark *placekmark = [placemarks firstObject];
    
           userLocation.title = placekmark.name;
           userLocation.subtitle = placekmark.locality;
    
        }];
      6.3 效果如下图


   }
6.2调用的方法 6.3反地理编码之后的值 关于打大头针的知识

具体的代码如下:

提示:(运行下面的代码之前记得在info.plist里面进行权限配置)
#import "ViewController.h"
#import <MapKit/MapKit.h>
@interface ViewController ()<MKMapViewDelegate>

@property(nonatomic,strong) MKMapView *mapView;

@property(nonatomic,strong)  CLLocationManager *locationManger;

@property(nonatomic,strong) CLGeocoder *geocoder;


@end

@implementation ViewController

//1.显示地图
-(MKMapView *)mapView
{
if (!_mapView) {
    
    _mapView = [[MKMapView alloc]initWithFrame:self.view.bounds];
    
    [self.view addSubview:_mapView];
}

return _mapView;
}

- (void)viewDidLoad {
[super viewDidLoad];

//2.设置地图显示类型
self.mapView.mapType = MKMapTypeStandard;

/*
 
   MKMapTypeStandard  标准类型
   MKMapTypeSatellite, 卫星图
   MKMapTypeHybrid, 混合的图(标准+卫星)
   MKMapTypeSatelliteFlyover
   MKMapTypeHybridFlyover
 
 */

//3用户定位权限跟踪设置

[self.locationManger requestAlwaysAuthorization];

//4.如果想利用MapView获取用户的位置,可以追踪用户的诶之

self.mapView.userTrackingMode = MKUserTrackingModeFollow;

/*
 
 MKUserTrackingModeNone = 0, 不追踪,不准确
 MKUserTrackingModeFollow, 追踪
 MKUserTrackingModeFollowWithHeading,  追踪并且获取用户方向
 
 */

//注意在IOS8里面想要追踪用户的位置,必须自己请求隐私权限

//5.关于地图的一些其他设置

//mapView.rotateEnabled = YES;

/*
 
 scrollEnabled  支持拖动
 
 rotateEnabled  支持旋转
 
 pitchEnabled   支持缩放
 
 */

//mapView.showsUserLocation = YES;

//6.锁定位置直接移动到指定位置

self.mapView.delegate = self;

}

#pragma mark MKMapViewDelegate
/*
 *  每次更新到用户的位置就会调用(调用不 频繁,只有位置改变才会调用)
 *
 *  @param mapView  促发事件的控件
 *
 *  @param userLocation  大头针模型
 *
 */
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
/*
 
  地图上蓝色的点就称之为大头针
  大头针可以拥有标题/子标题/位置信息

  MKUserLocation 大头针是由大头针的模型来确定
 
 */
//6.1设置大头针显示的内容
//    userLocation.title = @"蓝科";
//    userLocation.subtitle = @"牛逼";

//6.2移动地图到用户所在的位置
//获取用户当前所在位置的经纬度,并且设置为地图的中心点
//[self.mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];

//6.3设置地图显示的区域

//获取用户的位置
//    
//    CLLocationCoordinate2D center = userLocation.location.coordinate;
//    
//    //将用户当前的位置作为显示区域的中心点,并且指定需要显示的跨度范围
//    //
//    MKCoordinateSpan span = MKCoordinateSpanMake(100, 100);
//    MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
//    
//    //设置显示区域
//    [self.mapView setRegion:region animated:YES];
//

//利用饭地理编码获取位置之后设置标题


[self.geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
    
    CLPlacemark *placekmark = [placemarks firstObject];
    
    userLocation.title = placekmark.name;
    userLocation.subtitle = placekmark.locality;
    
    NSLog(@"==%@ ==%@",userLocation.title, userLocation.subtitle);
    
}];

}

-(CLGeocoder *)geocoder
{
if (!_geocoder) {
    
    _geocoder = [[CLGeocoder alloc]init];
    
}

return _geocoder;
}

-(CLLocationManager *)locationManger
{

if (!_locationManger) {
    
    _locationManger = [[CLLocationManager alloc]init];
}

return _locationManger;
}

@end
总体结构图
上一篇下一篇

猜你喜欢

热点阅读