蘋果地圖定位MapKit

MapKit.framework
拖入地圖控件

設(shè)置
NSLocationWhenInUseUsageDescription yes

import "AppDelegate.h"

            #import <CoreLocation/CoreLocation.h>

            @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.
                // 程序一進(jìn)來,就請(qǐng)求授權(quán)
                if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0 ) {
                    [self.locationManager requestWhenInUseAuthorization];
                }

                return YES;
            }

import "ViewController.h"

        #import <MapKit/MapKit.h>

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

        @end

        @implementation ViewController

        - (void)viewDidLoad {
            [super viewDidLoad];
          
            
            // 地圖類型
        //    MKMapTypeStandard = 0, 默認(rèn) 標(biāo)準(zhǔn)
        //    MKMapTypeSatellite, 衛(wèi)星
        //    MKMapTypeHybrid 混合 = 標(biāo)準(zhǔn) + 衛(wèi)星
            self.mapView.mapType = MKMapTypeStandard;
            
            // 用戶位置跟蹤模式
        //    MKUserTrackingModeNone = 0, //用戶位置,不請(qǐng)?jiān)试S跟蹤
        //    MKUserTrackingModeFollow, // 用戶位置允許跟蹤
        //    MKUserTrackingModeFollowWithHeading,用戶位置允許跟蹤(方向)
            self.mapView.userTrackingMode = MKUserTrackingModeFollow;
            
            // 設(shè)置mapView代理
            self.mapView.delegate = self;
            

        }


        #pragma mark 定位到當(dāng)前用戶位置
        -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
            // 1.當(dāng)前位置詳細(xì)描述
            userLocation.title = @"廣州";
            userLocation.subtitle = @"天河";
        #warning 自己測(cè)試
            //當(dāng)前的位置詳細(xì)描述,要顯示哪個(gè)城市,哪個(gè)區(qū)-(反地理編碼)
            
            
            // 2.設(shè)置顯示的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 在此方法, 動(dòng)畫效果不起作用,其它方法方法可以
            //[self.mapView setRegion:region animated:YES];

            

        }


        #pragma 地圖顯示的區(qū)域改變
        -(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
            MKCoordinateSpan span = self.mapView.region.span;

            NSLog(@"區(qū)域 經(jīng)度差值: %lf 緯度差值: %lf", span.longitudeDelta,span.latitudeDelta);
        }


        #pragma 返回當(dāng)前位置
        - (IBAction)backCurrentLocation{


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

        @end
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容