#pragma mark - 創(chuàng)建地圖
-(void)createMapView
{
_mapView = [[MAMapView alloc]initWithFrame:self.view.frame];
[self.view addSubview:_mapView];
//設(shè)置代理
_mapView.delegate = self;
/*高德地圖涉及到的一些屬性*/
//1.設(shè)置是否顯示底層的建筑名稱標注
_mapView.showsLabels = YES;
//2.設(shè)置地圖的類型,分為衛(wèi)星地圖黄橘,標準地圖和夜景地圖
_mapView.mapType = MAMapTypeStandard;
//3.設(shè)置是否顯示交通狀況
_mapView.showTraffic = YES;
//4.設(shè)置顯示本人的位置,結(jié)合定位功能使用
_mapView.showsUserLocation = YES;
//5.設(shè)置地圖logo牛哺,默認字樣是“高德地圖”,用logoCenter來設(shè)置logo的位置
_mapView.logoCenter = CGPointMake(self.view.frame.size.width - 100, self.view.frame.size.height - 50);
//6.設(shè)置指南針compass,默認是開啟狀態(tài),大小是定值并闲,顯示在地圖的右上角
_mapView.showsCompass = YES;
_mapView.compassOrigin = self.view.center;
//7.設(shè)置比例尺scale,默認顯示在地圖的左上角
_mapView.showsScale = YES;
_mapView.scaleOrigin = CGPointMake(100, 100);
//8.設(shè)置地圖手勢
_mapView.zoomEnabled = YES;//開啟地圖手勢
//[_mapView setZoomLevel:15 animated:YES];
//9.設(shè)置是否開啟滑動手勢
_mapView.scrollEnabled = YES;
//10.設(shè)置是否開啟旋轉(zhuǎn)手勢
_mapView.rotateEnabled = YES;
//11.設(shè)置是否開啟傾斜手勢
_mapView.rotateCameraEnabled = YES;
}
#pragma mark - 在地圖上添加大頭針標注
-(void)addAnnotation
{
MAPointAnnotation * pointAnnotation = [[MAPointAnnotation alloc]init];
//設(shè)置大頭針需要標記的位置
pointAnnotation.coordinate = CLLocationCoordinate2DMake(39.989631, 116.481018);
//設(shè)置標題
pointAnnotation.title = @"方恒國際";
//設(shè)置副標題
pointAnnotation.subtitle = @"這是我家";
[_mapView addAnnotation:pointAnnotation];
}
#pragma mark - 設(shè)置大頭針的代理方法
-(MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
/*
//1.用系統(tǒng)的大頭針樣式
//大頭針也涉及到復(fù)用叹哭,它的復(fù)用跟UITableViewCell的復(fù)用機制一樣
//面向?qū)ο笳Z言的特性:封裝忍宋,繼承,多態(tài)(父類的指針可以指向子類的對象)
MAPinAnnotationView * pinAnotationView = (MAPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"mapView"];
//判斷復(fù)用池中是否有可用的對象风罩,如果沒有則創(chuàng)建
if (!pinAnotationView) {
pinAnotationView = [[MAPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"mapView"];
}
//設(shè)置氣泡可以顯示
pinAnotationView.canShowCallout = YES;
//設(shè)置大頭針降落的動畫
pinAnotationView.animatesDrop = YES;
//設(shè)置大頭針的顏色
pinAnotationView.pinColor = MAPinAnnotationColorPurple;
//設(shè)置大頭針是否可以拖拽
pinAnotationView.draggable = YES;
return pinAnotationView;
*/
//2.使用自定義的大頭針樣式
MAAnnotationView * annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"mapView"];
if (!annotationView) {
annotationView = [[MAAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"mapView"];
}
//設(shè)置大頭針的樣式為一張圖片
annotationView.image = [UIImage imageNamed:@"pig1"];
//讓氣泡顯示
annotationView.canShowCallout = YES;
//設(shè)置是否拖拽
annotationView.draggable = YES;
return annotationView;
}
#pragma mark- 畫折線 糠排,一般用于路徑規(guī)劃
-(void)drawLine
{
//構(gòu)造折線的數(shù)據(jù)點
CLLocationCoordinate2D commonPolylineCoords[4];
commonPolylineCoords[0].latitude = 39.832136;
commonPolylineCoords[0].longitude = 116.34095;
commonPolylineCoords[1].latitude = 39.832136;
commonPolylineCoords[1].longitude = 116.42095;
commonPolylineCoords[2].latitude = 39.902136;
commonPolylineCoords[2].longitude = 116.42095;
commonPolylineCoords[3].latitude = 39.902136;
commonPolylineCoords[3].longitude = 116.44095;
//構(gòu)造折線對象
//參數(shù)一:多個點組成的類似數(shù)組的經(jīng)緯度數(shù)據(jù)
//參數(shù)二:構(gòu)成折線總共涉及到幾個點
MAPolyline * polyLine = [MAPolyline polylineWithCoordinates:commonPolylineCoords count:4];
//將覆蓋圖層添加到地圖上
[_mapView addOverlay:polyLine];
}
#pragma mark - 畫多邊形,一般用于精確的搜索
-(void)drawGon
{
//構(gòu)造多邊形數(shù)據(jù)
CLLocationCoordinate2D coordinates[4];
coordinates[0].latitude = 39.810892;
coordinates[0].longitude = 116.233413;
coordinates[1].latitude = 39.816600;
coordinates[1].longitude = 116.331842;
coordinates[2].latitude = 39.762187;
coordinates[2].longitude = 116.357932;
coordinates[3].latitude = 39.733653;
coordinates[3].longitude = 116.278255;
MAPolygon * polyGon = [MAPolygon polygonWithCoordinates:coordinates count:4];
[_mapView addOverlay:polyGon];
}
#pragma mark - 畫圓超升,一般用于模糊搜索
-(void)drawCircle
{
//需要原點和半徑
MACircle * circle = [MACircle circleWithCenterCoordinate:CLLocationCoordinate2DMake(39.952136, 116.50095) radius:5000];
[_mapView addOverlay:circle];
}
#pragma mark - 實現(xiàn)覆蓋圖層樣式的代理方法
-(MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id<MAOverlay>)overlay
{
//舊版的返回值是MAOverLayView入宦,新版的是MAOverlayRenderer
//畫折線
if ([overlay isKindOfClass:[MAPolyline class]]) {
MAPolylineRenderer * polyLine = [[MAPolylineRenderer alloc]initWithPolyline:overlay];
//設(shè)置屬性
//設(shè)置線寬
polyLine.lineWidth = 5;
//設(shè)置填充顏色
polyLine.fillColor = [UIColor redColor];
//設(shè)置筆觸顏色
polyLine.strokeColor = [UIColor greenColor];
//設(shè)置斷點類型
polyLine.lineCapType = kMALineCapRound;
return polyLine;
} else if([overlay isKindOfClass:[MAPolygon class]])
{
MAPolygonRenderer * polyGon = [[MAPolygonRenderer alloc]initWithPolygon:overlay];
polyGon.fillColor = [UIColor redColor];
polyGon.strokeColor = [UIColor yellowColor];
//設(shè)置顯示未虛線
polyGon.lineDash = YES;
polyGon.lineWidth = 8;
return polyGon;
} else if([overlay isKindOfClass:[MACircle class ]])
{
MACircleRenderer * circle = [[MACircleRenderer alloc]initWithCircle:overlay];
circle.fillColor = [UIColor blueColor];
circle.strokeColor = [UIColor yellowColor];
circle.lineWidth = 5;
circle.lineDash = YES;
return circle;
}
return nil;
}
定位
- (void)viewDidLoad {
[super viewDidLoad];
//配置用戶key
[AMapLocationServices sharedServices].apiKey = @"e51bdf2a028ff1a08da5f20f114ff3c0";
//初始化定位
_manager = [[AMapLocationManager alloc]init];
//設(shè)置代理
_manager.delegate = self;
//開啟定位
[_manager startUpdatingLocation];
}
//定位的目的是為了拿到經(jīng)緯度值,iOS8之后更新的定位代理方法
-(void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location
{
NSLog(@"經(jīng)緯度值~~~%f---%f",location.coordinate.longitude,location.coordinate.latitude);
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者