苹果地图定位MapKit

MapKit.framework
拖入地图控件

设置
NSLocationWhenInUseUsageDescription yes

import "AppDelegate.h"

            #import 

            @interface AppDelegate ()
            @property (nonatomic,strong) CLLocationManager *locationManager;
            @end

            @implementation AppDelegate




            -(CLLocationManager *)locationManager{
                if (!_locationManager) {
                    _locationManager = [[CLLocationManager alloc] init];
                }
                
                return _locationManager;
            }


            - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
                // Override point for customization after application launch.
                // 程序一进来,就请求授权
                if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0 ) {
                    [self.locationManager requestWhenInUseAuthorization];
                }

                return YES;
            }

import "ViewController.h"

        #import 

        @interface ViewController ()
        @property (weak, nonatomic) IBOutlet MKMapView *mapView;

        @end

        @implementation ViewController

        - (void)viewDidLoad {
            [super viewDidLoad];
          
            
            // 地图类型
        //    MKMapTypeStandard = 0, 默认 标准
        //    MKMapTypeSatellite, 卫星
        //    MKMapTypeHybrid 混合 = 标准 + 卫星
            self.mapView.mapType = MKMapTypeStandard;
            
            // 用户位置跟踪模式
        //    MKUserTrackingModeNone = 0, //用户位置,不请允许跟踪
        //    MKUserTrackingModeFollow, // 用户位置允许跟踪
        //    MKUserTrackingModeFollowWithHeading,用户位置允许跟踪(方向)
            self.mapView.userTrackingMode = MKUserTrackingModeFollow;
            
            // 设置mapView代理
            self.mapView.delegate = self;
            

        }


        #pragma mark 定位到当前用户位置
        -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
            // 1.当前位置详细描述
            userLocation.title = @"广州";
            userLocation.subtitle = @"天河";
        #warning 自己测试
            //当前的位置详细描述,要显示哪个城市,哪个区-(反地理编码)
            
            
            // 2.设置显示的region
            //MKCoordinateSpan span = MKCoordinateSpanMake(0.193626, 0.145513);
            MKCoordinateSpan span = MKCoordinateSpanMake(0.085125, 0.015596);
            MKCoordinateRegion region = MKCoordinateRegionMake(self.mapView.userLocation.coordinate, span);
            self.mapView.region = region;
        #pragma mark 在此方法, 动画效果不起作用,其它方法方法可以
            //[self.mapView setRegion:region animated:YES];

            

        }


        #pragma 地图显示的区域改变
        -(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
            MKCoordinateSpan span = self.mapView.region.span;

            NSLog(@"区域 经度差值: %lf 纬度差值: %lf", span.longitudeDelta,span.latitudeDelta);
        }


        #pragma 返回当前位置
        - (IBAction)backCurrentLocation{


            MKCoordinateSpan span = MKCoordinateSpanMake(0.063659, 0.047845);
            MKCoordinateRegion region = MKCoordinateRegionMake(self.mapView.userLocation.coordinate, span);
            
            //这里就可以设置
            [self.mapView setRegion:region animated:YES];
            
            
            //[self.mapView setCenterCoordinate:self.mapView.userLocation.coordinate animated:YES];
        }

        @end

你可能感兴趣的:(苹果地图定位MapKit)