1. 程式人生 > >帶有當前位置座標的地圖

帶有當前位置座標的地圖

匯入標頭檔案

//匯入系統類
#import <MapKit/MapKit.h>
//匯入獲取經緯度類
#import <CoreLocation/CoreLocation.h>
//設定協議

@interface MapUIkit ()

<MKMapViewDelegate,CLLocationManagerDelegate>

//建立地圖物件
@property(nonatomic,strong)MKMapView *MapView;
@property(nonatomic,strong)CLLocationManager *lomanger;

@end

@implementation MapUIkit

  • (void)viewDidLoad {

    [super viewDidLoad];
    //設定導航標題
    self.navigationItem.title =self.provie;
    //設定右側按鈕
    //self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@“省會定位” style:UIBarButtonItemStyleDone target:self action:@selector(YOU:)];
    //例項化
    self.MapView = [[MKMapView alloc] initWithFrame:self.view.frame];
    //設定代理
    self.MapView.delegate = self;
    //設定地圖樣式
    self.MapView.mapType = MKMapTypeStandard;
    //載入檢視
    [self.view addSubview:self.MapView];
    //設定按鈕
    UIButton *butt = [UIButton buttonWithType:UIButtonTypeCustom];
    //設定
    butt.frame = CGRectMake(40, 600,50, 50);
    //新增文字
    [butt setTitle:@“當前位置” forState:UIControlStateNormal];
    butt.titleLabel.font = [UIFont systemFontOfSize:10];
    //新增點選事件
    [butt addTarget:self action:@selector(dian:) forControlEvents:UIControlEventTouchUpInside];
    butt.layer.masksToBounds = YES;
    butt.layer.cornerRadius = 25;
    butt.backgroundColor = [UIColor greenColor];
    //新增檢視
    [self.view addSubview:butt];
    //例項化經緯度類
    self.lomanger = [[CLLocationManager alloc] init];
    //申請使用者授權使用者進入後臺不在授權
    [self.lomanger requestWhenInUseAuthorization];
    }

  • (void)YOU:(id)seb{
    //
    CLGeocoder *g = [[CLGeocoder alloc] init];
    //將地址字串轉換為位置的經緯度
    [g geocodeAddressString:self.provie completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
    //獲取位置隨便展示在地圖上
    CLPlacemark *place = [placemarks lastObject];
    //獲取位置
    CLLocation *loc =place.location;
    CLLocationCoordinate2D coor = loc.coordinate;
    //定位
    MKPointAnnotation *anne = [[MKPointAnnotation alloc] init];
    //設定挫釘
    anne.coordinate = coor;
    //回到主執行緒
    dispatch_async(dispatch_get_main_queue(), ^{
    //設定讓地圖顯示區域縮小
    MKCoordinateRegion rgin =MKCoordinateRegionMakeWithDistance(coor, 10, 10);
    //新增到檢視
    [self.MapView setRegion:rgin];
    [self.MapView addAnnotation:anne];
    });
    }];
    }
    //實現點選方法

  • (void)dian:(id)sender{
    //設定代理
    self.lomanger.delegate = self;
    //開始獲取位置資訊 呼叫此方法後協議中的方法才會執行

    [self.lomanger startUpdatingLocation];
    }
    //實現代理方法

  • (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    //建立物件獲取當前獲取最後一個元素
    CLLocation *curloc = [locations lastObject];
    //建立結構體 獲取經緯度
    CLLocationCoordinate2D curCoor = curloc.coordinate;
    self.lomanger.delegate = nil;
    //輸出經緯度
    NSLog(@“經度%g,緯度%g”,curCoor.longitude,curCoor.latitude);

    //設定讓地圖顯示區域縮小
    MKCoordinateRegion rgin =MKCoordinateRegionMakeWithDistance(curCoor, 30, 30);
    //設定動畫並新增
    [self.MapView setRegion:rgin animated:YES];
    //將地址經緯度轉換為字串
    CLGeocoder *Geder = [[CLGeocoder alloc] init];
    //設定方法
    [Geder reverseGeocodeLocation:curloc completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
    //建立非同步佇列回到主執行緒
    dispatch_async(dispatch_get_main_queue(), ^{
    //獲取最後一個經緯度轉換為字串
    CLPlacemark *place = [placemarks firstObject];
    //設定大頭針
    MKPointAnnotation *pino = [[MKPointAnnotation alloc] init];
    //將獲取的地址名字給大頭針
    pino.title = place.name;
    //設定大頭針的位置
    pino.coordinate = curCoor;
    //新增到地圖上
    [self.MapView addAnnotation:pino];
    });
    }];
    }
    //實現大頭針點選事件

  • (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation{
    //從MAPView找一塊可用的記憶體
    MKPinAnnotationView *kl =[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@“1”];
    //設定動畫
    kl.animatesDrop = YES;
    //設定
    kl.canShowCallout = YES;
    //返回內容
    return kl;

}
@end