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