定位和地圖可以分開使用
配置時(shí)需要條件編譯
//地圖的頭文件
#import
//定位的頭文件
#import
//自定義注釋類(繼承于MKAnnotation,去除readyonly后的coordinate,title,subtitle)
#import "CustomAnnotation.h"
//創(chuàng)建地圖
MKMapView *map = [[MKMapView alloc] initWithFrame:self.view.bounds];
//顯示樣式(系統(tǒng)地圖高德地圖)
map.mapType = MKMapTypeStandard;
//經(jīng)緯度
//CLLocationCoordinate2D coor = CLLocationCoordinate2DMake(26.548277, 148.125381);
//設(shè)置地圖中心點(diǎn)
//map.centerCoordinate = coor;
//初顯示范圍設(shè)置要顯示的區(qū)域
//MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coor, 100000, 100000);
//[map setRegion:region animated:YES];
//map.scrollEnabled = NO;滑動(dòng)
//map.zoomEnabled = NO;縮放
//左上角比例尺,多放大幾次屏幕就顯示
map.showsScale = YES;
//旋轉(zhuǎn)時(shí)右上角的指南針(羅盤)默認(rèn)YES顯示
//map.showsCompass = NO;
//設(shè)置代理MKMapViewDelegate
map.delegate = self;
//第一次加載模擬器不顯示位置,更新位置就顯示,如果是真機(jī),第一次就可以顯示
//運(yùn)行后自己再更新位置
map.showsUserLocation = YES;
[self.view addSubview:map];
//創(chuàng)建定位對(duì)象
manager = [[CLLocationManager alloc] init];
//授權(quán)定位
[manager requestAlwaysAuthorization];
//要去info.plist文件配置NSLocationAlwaysUsageDescription
//創(chuàng)建自定義注釋對(duì)象
CustomAnnotation *ann = [[CustomAnnotation alloc] init];
ann.coordinate=CLLocationCoordinate2DMake(41.5427, 116.2617);
ann.title=@"北京";
ann.subtitle=@"五環(huán)";
//標(biāo)注紅色圓點(diǎn)
[map addAnnotation:ann];
//繪制路徑
//MKCircle圓圈MKPolyline折線MKPolygon閉合
//閉合區(qū)域
//坐標(biāo)點(diǎn)數(shù)組里面有四個(gè)點(diǎn)
CLLocationCoordinate2Dpoints [4];
points[0] = CLLocationCoordinate2DMake(39, 100);
points[1] = CLLocationCoordinate2DMake(39, 110);
points[2] = CLLocationCoordinate2DMake(29, 110);
points[3] = CLLocationCoordinate2DMake(29, 100);
//MKPolygon *gon = [MKPolygon polygonWithCoordinates:points count:4];
//[map addOverlay:gon];
//MKPolyline *line = [MKPolyline polylineWithCoordinates:points count:4];
//[map addOverlay:line];
MKCircle *circle = [MKCircle circleWithCenterCoordinate:points[3] radius:1000000.00];
[map addOverlay:circle];
}
#pragma mark -- MKMapViewDelegate
//對(duì)繪制區(qū)域的圖層進(jìn)行渲染圖層才能顯示
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id)overlay {
if([overlay isMemberOfClass:[MKPolygon class]]) {
//渲染對(duì)象
MKPolygonRenderer * renderer = [[MKPolygonRenderer alloc] initWithPolygon:overlay];
//線寬
renderer.lineWidth= 5;
//線的顏色
renderer.strokeColor= [UIColor redColor];
//填充色
renderer.fillColor= [UIColor cyanColor];
return renderer;
} elseif ([overlay isKindOfClass:[MKPolyline class]]){
MKPolylineRenderer *render = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
render.lineWidth = 5;
render.strokeColor = [UIColor orangeColor];
render.fillColor = [UIColor redColor];
return render;
} else {
MKCircleRenderer *render = [[MKCircleRenderer alloc] initWithCircle:overlay];
render.lineWidth = 1;
render.strokeColor = [UIColor purpleColor];
render.fillColor = [UIColor yellowColor];
return render;
}
}
//更新用戶位置的時(shí)候調(diào)用
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocationNS_AVAILABLE(10_9, 4_0) {
//初顯示范圍設(shè)置要顯示的區(qū)域
MKCoordinateRegion regin =MKCoordinateRegionMakeWithDistance(userLocation.location.coordinate, 100000, 100000);
//要設(shè)置才能觸發(fā)
[mapView setRegion:regin animated:YES];
//更新位置點(diǎn)擊圖標(biāo)顯示
mapView.userLocation.title = @"福州";
mapView.userLocation.subtitle = @"hot";
}
//自定義彈出的氣泡
- (nullableMKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {
//如果是自定義的大頭針就處理泡泡顯示的樣式
if([annotation isKindOfClass:[CustomAnnotation class]]) {
//使用MKAnnotationView不能實(shí)現(xiàn)animatesDrop所有使用其子類MKPinAnnotationView
MKPinAnnotationView *ann = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:NSStringFromClass([MKPinAnnotationView class])];
if(!ann) {
ann = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:NSStringFromClass([MKPinAnnotationView class])];
//美化]
//大頭針的背景色
ann.pinTintColor= [UIColor purpleColor];
//掉落的動(dòng)畫(從上往下的效果)
ann.animatesDrop =YES;
//可以響應(yīng)(彈出氣泡)
ann.canShowCallout =YES;
//彈出氣泡的左側(cè)的view
ann.leftCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeInfoLight];
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1"]];
image.frame = CGRectMake(0, 0, 32, 32);
ann.rightCalloutAccessoryView = image;
}
return ann;
} else {//否則不處理返回系統(tǒng)自帶樣式
return nil;
}
}
//點(diǎn)擊氣泡上的按鈕觸發(fā)的方法
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
NSLog(@"tap ===");
}
//自定義注釋類要導(dǎo)入這個(gè)頭文件
#import
//MKAnnotation
@interface CustomAnnotation :NSObject
//去掉readyonly
//必選
@property (nonatomic,assign) CLLocationCoordinate2D coordinate;
//可選
@property (nonatomic,copy) NSString *title;
@property (nonatomic,copy) NSString *subtitle;
@implementation CustomAnnotation
//MRC ?set方法
//- (void)setTitle:(NSString *)title {
// ???//判斷新舊值是否相等嚴(yán)謹(jǐn)
// ???if (_title != title) {
// ???????//釋放老對(duì)象的所有權(quán)
// ???????[_title release];
// ???????//為防止新對(duì)象釋放掉要引用一次
// ???????[title retain];
// ???????//重新賦值(目的)
// ???????_title = title;
// ???}
//}
@end