地圖Map

- (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;

? ? }

}

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

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

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