庫
coreMotion.framework
CoreLocation.framework
MapKit.framework
第一個頁面
#import<MapKit/MapKit.h>
#import< CoreLocation/CoreLocation.h>
<MKMapViewDelegate,CLLocationManagerDelegate>
{
CLLocationManager *manager;
}
@property(nonatomic,strong)MKMapView *mapView;
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 實(shí)例化地圖視圖
self.mapView = [[MKMapView alloc] initWithFrame:self.view.frame];
// 設(shè)置地圖類型
self.mapView.mapType = MKMapTypeHybrid; // 混合地圖
self.mapView.mapType = MKMapTypeSatellite; // 衛(wèi)星圖
self.mapView.mapType = MKMapTypeStandard; // 平面地圖
// 設(shè)置地圖可以滾動
self.mapView.scrollEnabled = YES;
// 設(shè)置可以嚙捏合
self.mapView.pitchEnabled = YES;
// 設(shè)置可以旋轉(zhuǎn)
self.mapView.rotateEnabled? = YES;
// 設(shè)置可以點(diǎn)擊縮放
self.mapView.zoomEnabled = YES;
// 設(shè)置委托
self.mapView.delegate = self;
// 設(shè)置可以顯示用戶當(dāng)前位置
self.mapView.showsUserLocation = YES;
//
[self.view addSubview:self.mapView];
// 盜用CLLocationManager獲取當(dāng)前位置
manager = [[CLLocationManager alloc] init];
//距離過濾
manager.distanceFilter = 10;
manager.desiredAccuracy = kCLLocationAccuracyBest;
manager.delegate = self;
manager.pausesLocationUpdatesAutomatically = YES;
[manager setActivityType:CLActivityTypeOther];
[manager requestAlwaysAuthorization];
// 添加“長按手勢”片橡,觸發(fā)后可以顯示當(dāng)前位置扑庞,
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(showCurrentLocation:)];
[self.mapView addGestureRecognizer:longPress];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(getPointToCoordinate:)];
[self.mapView addGestureRecognizer:tap];
-(void)getPointToCoordinate:(UITapGestureRecognizer *)reco
{
// 北京八維研修學(xué)院
// 獲得手勢點(diǎn)擊的坐標(biāo)
CGPoint point = [reco locationInView:self.mapView];
// 把坐標(biāo)點(diǎn)轉(zhuǎn)換為經(jīng)緯度信息
CLLocationCoordinate2D coor = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
NSLog(@"緯度:%g,經(jīng)度:%g",coor.latitude,coor.longitude);
// 添加錨點(diǎn)
MKPointAnnotation *pointAnno = [[MKPointAnnotation alloc] init];
pointAnno.coordinate = coor;
pointAnno.title = [NSString stringWithFormat:@"緯度:%g,經(jīng)度:%g",coor.latitude,coor.longitude];
// 根據(jù)經(jīng)緯度解析為地址字符串()
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
CLLocation *location = [[CLLocation alloc] initWithLatitude:coor.latitude longitude:coor.longitude];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placeMark = [placemarks lastObject];
pointAnno.subtitle = placeMark.name;
dispatch_async(dispatch_get_main_queue(), ^{
[self.mapView addAnnotation:pointAnno];
});
}];
}
//
-(void)showCurrentLocation:(UILongPressGestureRecognizer *)reco
{
[manager startUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *currentLocation = [locations lastObject];
// 設(shè)置顯示范圍没隘,參數(shù)值越小越精確
MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);
// 設(shè)置顯示區(qū)域(包括中心點(diǎn)和范圍)
MKCoordinateRegion region = MKCoordinateRegionMake(currentLocation.coordinate, span);
// 把表示區(qū)域的圓形區(qū)域轉(zhuǎn)換為一個符合地理方圓的區(qū)域
MKCoordinateRegion fitRegion = [self.mapView regionThatFits:region];
// 讓地圖顯示當(dāng)前位置
[self.mapView setRegion:fitRegion animated:YES];
}
#pragma mark - MKMapViewDelegate
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{
NSLog(@"顯示區(qū)域?qū)⒁兓?);
}
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
NSLog(@"顯示區(qū)域變化完成");
}
- (void)mapViewWillStartLoadingMap:(MKMapView *)mapView
{
NSLog(@"將要加載地圖");
}
- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView
{
NSLog(@"加載地圖完成");
}
- (void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error
{
NSLog(@"加載地圖失敗");
}
- (void)mapViewWillStartRenderingMap:(MKMapView *)mapView NS_AVAILABLE(10_9, 7_0)
{
NSLog(@"將要渲染地圖");
}
- (void)mapViewDidFinishRenderingMap:(MKMapView *)mapView fullyRendered:(BOOL)fullyRendered NS_AVAILABLE(10_9, 7_0)
{
NSLog(@"渲染地圖完成");
}