#import "ViewController.h"
#import
#import
@interface ViewController ()
@property(nonatomic,strong)MAMapView * mapView;
@property(nonatomic,strong)CLLocationManager * CLmanager;
@end
@implementation ViewController
-(MAMapView* )mapView
{
if(!_mapView)
{
///初始化地图
_mapView=[[MAMapViewalloc]initWithFrame:self.view.bounds];
//代理
_mapView.delegate=self;
///如果您需要进入地图就显示定位小蓝点,则需要下面两行代码
_mapView.showsUserLocation = YES;
_mapView.userTrackingMode = MAUserTrackingModeFollow;
}
return _mapView;
}
-(CLLocationManager * )CLmanager
{
if(!_CLmanager)
{
_CLmanager=[CLLocationManager new];
_CLmanager.delegate=self;
}
return _CLmanager;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title=@"高德地图";
self.navigationController.navigationBar.translucent=NO;
[AMapServices sharedServices].enableHTTPS = YES;
///把地图添加至view
[self.viewaddSubview:self.mapView];
[_CLmanager requestWhenInUseAuthorization];
self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"定位" style:UIBarButtonItemStylePlain target:self action:@selector(dingwei)];
}
-(void)dingwei
{
//开始跟新迪丽位置
[self.CLmanager startUpdatingLocation];
MAUserLocationRepresentation *r = [[MAUserLocationRepresentation alloc] init];
r.showsAccuracyRing = NO;///精度圈是否显示,默认YES
r.fillColor = [UIColor redColor];///精度圈 填充颜色, 默认 kAccuracyCircleDefaultColor
r.strokeColor = [UIColor blueColor];///精度圈 边线颜色, 默认 kAccuracyCircleDefaultColor
r.lineWidth = 10;///精度圈 边线宽度,默认0
[self.mapView updateUserLocationRepresentation:r];
}
-(void)locationManager:(CLLocationManager*)manager didUpdateLocations:(NSArray *)locations
{
//获取当前位置
CLLocation* curlocation=[locationslastObject];
//获取当前位置的金纬度
CLLocationCoordinate2D curCoor=curlocation.coordinate;
dispatch_async(dispatch_get_main_queue(), ^{
//设置地图显示的区域
MACoordinateRegion showRegion=MACoordinateRegionMakeWithDistance(curCoor,1100,1100);
[self.mapViewsetRegion: showRegionanimated:YES];
//实例化一个大头针
MAPointAnnotation * point=[[MAPointAnnotation alloc]init];
point.coordinate=curCoor;
point.title=@"当前位置";
//迪丽位置解析
CLGeocoder* geocoder=[[CLGeocoderalloc]init];
[geocoderreverseGeocodeLocation:curlocationcompletionHandler:^(NSArray *_Nullableplacemarks,NSError*_Nullableerror) {
CLPlacemark* place=[placemarkslastObject];
point.subtitle=place.name;
//回到UI主线程,给地图添加注释视图
dispatch_async(dispatch_get_main_queue(), ^{
[self.mapViewaddAnnotation:point];
self.CLmanager=nil;
});
}];
});
}
- (MAAnnotationView*)mapView:(MAMapView*)mapView viewForAnnotation:(id)annotation
{
if([annotationisKindOfClass:[MAPointAnnotationclass]])
{
staticNSString*pointReuseIndentifier =@"pointReuseIndentifier";
MAPinAnnotationView*annotationView = (MAPinAnnotationView*)[mapViewdequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
if(annotationView ==nil)
{
annotationView = [[MAPinAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:pointReuseIndentifier];
}
annotationView.canShowCallout=YES; //设置气泡可以弹出,默认为NO
annotationView.animatesDrop=YES; //设置标注动画显示,默认为NO
annotationView.draggable=YES; //设置标注可以拖动,默认为NO
annotationView.pinColor=MAPinAnnotationColorPurple;
returnannotationView;
}
return nil;
}
@end