#pragma mark - 創(chuàng)建地圖
-(void)createMapView
{
_mapView = [[MAMapView alloc]initWithFrame:self.view.frame];
[self.view addSubview:_mapView];
//設置代理
_mapView.delegate = self;
/*高德地圖涉及到的一些屬性*/
//1.設置是否顯示底層的建筑名稱標注
_mapView.showsLabels = YES;
//2.設置地圖的類型,分為衛(wèi)星地圖,標準地圖和夜景地圖
_mapView.mapType = MAMapTypeStandard;
//3.設置是否顯示交通狀況
_mapView.showTraffic = YES;
//4.設置顯示本人的位置,結合定位功能使用
_mapView.showsUserLocation = YES;
//5.設置地圖logo,默認字樣是“高德地圖”,用logoCenter來設置logo的位置
_mapView.logoCenter = CGPointMake(self.view.frame.size.width - 100, self.view.frame.size.height - 50);
//6.設置指南針compass,默認是開啟狀態(tài),大小是定值,顯示在地圖的右上角
_mapView.showsCompass = YES;
_mapView.compassOrigin = self.view.center;
//7.設置比例尺scale,默認顯示在地圖的左上角
_mapView.showsScale = YES;
_mapView.scaleOrigin = CGPointMake(100, 100);
//8.設置地圖手勢
_mapView.zoomEnabled = YES;//開啟地圖手勢
//[_mapView setZoomLevel:15 animated:YES];
//9.設置是否開啟滑動手勢
_mapView.scrollEnabled = YES;
//10.設置是否開啟旋轉手勢
_mapView.rotateEnabled = YES;
//11.設置是否開啟傾斜手勢
_mapView.rotateCameraEnabled = YES;
}
#pragma mark - 在地圖上添加大頭針標注
-(void)addAnnotation
{
MAPointAnnotation * pointAnnotation = [[MAPointAnnotation alloc]init];
//設置大頭針需要標記的位置
pointAnnotation.coordinate = CLLocationCoordinate2DMake(39.989631, 116.481018);
//設置標題
pointAnnotation.title = @"方恒國際";
//設置副標題
pointAnnotation.subtitle = @"這是我家";
[_mapView addAnnotation:pointAnnotation];
}
#pragma mark - 設置大頭針的代理方法
-(MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
/*
//1.用系統(tǒng)的大頭針樣式
//大頭針也涉及到復用,它的復用跟UITableViewCell的復用機制一樣
//面向對象語言的特性:封裝,繼承,多態(tài)(父類的指針可以指向子類的對象)
MAPinAnnotationView * pinAnotationView = (MAPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"mapView"];
//判斷復用池中是否有可用的對象,如果沒有則創(chuàng)建
if (!pinAnotationView) {
pinAnotationView = [[MAPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"mapView"];
}
//設置氣泡可以顯示
pinAnotationView.canShowCallout = YES;
//設置大頭針降落的動畫
pinAnotationView.animatesDrop = YES;
//設置大頭針的顏色
pinAnotationView.pinColor = MAPinAnnotationColorPurple;
//設置大頭針是否可以拖拽
pinAnotationView.draggable = YES;
return pinAnotationView;
*/
//2.使用自定義的大頭針樣式
MAAnnotationView * annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"mapView"];
if (!annotationView) {
annotationView = [[MAAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"mapView"];
}
//設置大頭針的樣式為一張圖片
annotationView.image = [UIImage imageNamed:@"pig1"];
//讓氣泡顯示
annotationView.canShowCallout = YES;
//設置是否拖拽
annotationView.draggable = YES;
return annotationView;
}
#pragma mark- 畫折線 ,一般用于路徑規(guī)劃
-(void)drawLine
{
//構造折線的數(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;
//構造折線對象
//參數(shù)一:多個點組成的類似數(shù)組的經(jīng)緯度數(shù)據(jù)
//參數(shù)二:構成折線總共涉及到幾個點
MAPolyline * polyLine = [MAPolyline polylineWithCoordinates:commonPolylineCoords count:4];
//將覆蓋圖層添加到地圖上
[_mapView addOverlay:polyLine];
}
#pragma mark - 畫多邊形,一般用于精確的搜索
-(void)drawGon
{
//構造多邊形數(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];
//設置屬性
//設置線寬
polyLine.lineWidth = 5;
//設置填充顏色
polyLine.fillColor = [UIColor redColor];
//設置筆觸顏色
polyLine.strokeColor = [UIColor greenColor];
//設置斷點類型
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];
//設置顯示未虛線
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];
//設置代理
_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);
}
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。