
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? /**創(chuàng)建地圖*/
? ? [self creatMap];
? ? /**定位當(dāng)前*/? // 模擬器上無法定位
? ? [self selfLocation];
}
#pragma mark定位自己
- (void)selfLocation{
? ? // 創(chuàng)建定位對象
? ? _locationManager = [[CLLocationManager alloc]init];
? ? // 設(shè)置定位屬性
? ? _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
? ? // 設(shè)置定位更新距離
? ? _locationManager.distanceFilter = 10.0;
? ? // 綁定定位委托
? ? _locationManager.delegate = self;
? ? //取出當(dāng)前應(yīng)用的定位服務(wù)狀態(tài)(枚舉值)
? ? CLAuthorizationStatus status =[CLLocationManager authorizationStatus];
? ? //如果未授權(quán)則請求
? ? if(status==kCLAuthorizationStatusNotDetermined) {
? ? ? ? [_locationManager requestAlwaysAuthorization];
? ? }
? ? // 開始定位
? ? [_locationManager startUpdatingLocation];
}
#pragma mark CLLocationManagerDelegate
// 定位后的返回結(jié)果
- (void)locationManager:(CLLocationManager*)manager didUpdateLocations:(NSArray *)locations{
? ? //locations數(shù)組中存儲的是CLLocation
? ? //遍歷數(shù)組取出坐標(biāo)--》如果不需要也可以不遍歷
? ? CLLocation*location =[locationsfirstObject];
?? ? MKCoordinateRegion region =MKCoordinateRegionMake(location.coordinate, MKCoordinateSpanMake(0.01,0.01));
? ? [_mapView setRegion:region animated:YES];
? ? // 創(chuàng)建大頭針
? ? MKPointAnnotation *point = [[MKPointAnnotation alloc]init];
? ? // 設(shè)置經(jīng)緯度
? ? point.coordinate= location.coordinate;
? ? // 設(shè)置主標(biāo)題
? ? point.title = @"The location of the I";
? ? // 設(shè)置副標(biāo)題
? ? point.subtitle=@"Hello Map";
? ? // 將大頭針添加到地圖上
? ? [_mapViewaddAnnotation:point];
}
#pragma mark創(chuàng)建地圖
-(void)creatMap{
? ? // 實例化
? ? _mapView = [[MKMapView alloc]initWithFrame:self.view.frame];
? ? // 是否可以縮放
? ? _mapView.zoomEnabled = YES;
? ? // 是否可以滾動
? ? _mapView.scrollEnabled = YES;
? ? // 是否可以旋轉(zhuǎn)
? ? _mapView.rotateEnabled = YES;
? ? // 是否顯示3D
? ? _mapView.pitchEnabled = YES;
? ? // 是否顯示指南針
?? ? _mapView.showsCompass = YES;
?? ? // 是否顯示比例尺
?? ? _mapView.showsScale = YES;
?? ? // 是否顯示交通
? ? _mapView.showsTraffic = YES;
?? ? // 是否顯示建筑物
?? ? _mapView.showsBuildings = YES;
?? ? //綁定委托
? ? _mapView.delegate=self;
? ? //添加到界面
? ? [self.view addSubview:_mapView];
? ? //添加手勢
? ? UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
?? ? //添加到地圖上
?? ? [_mapView addGestureRecognizer:longPress];
? ? //創(chuàng)建UISegmentedControl
? ? NSArray*mapTypeArray =@[@"標(biāo)準(zhǔn)",@"衛(wèi)星地圖",@"混合"];
? ? UISegmentedControl *segment =[[UISegmentedControl alloc] initWithItems:mapTypeArray];
?? ? segment.frame=CGRectMake(50,50,300,50);
?? ? [_mapViewaddSubview:segment];
? ? segment.selectedSegmentIndex=0;
}
- (void)longPress:(UILongPressGestureRecognizer *)sender{
? ? // 判斷是否長按
? ? if (sender.state != UIGestureRecognizerStateBegan) {
? ? ? ? return;
? ? }
}