前言
公司之前的定位模块,新功能需要用户自定义选点来设置安全围栏;高德地图iOS地图SDK没找到合适的方法,于是自己写了这个方法来实现,不多废话直接上代码;
集成高德地图SDK
这是高德开发者文档里提供的集成sdk的步骤:https://lbs.amap.com/api/ios-sdk/gettingstarted。
pod 'AMap3DMap', '~>6.3.0'
pod'AMapSearch' #搜索服务SDK
pod'AMapLocation'
pod'AMapNavi'#这个要放到其他高德sdk后
绘制地图定位到当前位置
self.mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_mapView.showsCompass= NO; // 设置成NO表示关闭指南针;YES表示显示指南针
_mapView.showsScale = YES;
//设置MapView的委托为自己
self.mapView.delegate = self;
[self.view addSubview:_mapView];
//如果您需要进入地图就显示定位小蓝点,则需要下面两行代码
//_mapView.customizeUserLocationAccuracyCircleRepresentation = YES;//是否自定义用户位置精度圈
_mapView.centerCoordinate = self.mapView.userLocation.coordinate;
_mapView.pausesLocationUpdatesAutomatically = NO;
_mapView.showsUserLocation = YES;
_mapView.userTrackingMode = MAUserTrackingModeFollow;
//NSLog(@"_mapView.userLocation = %@",_mapView.userLocation);
//设置定位精度
_mapView.desiredAccuracy = kCLLocationAccuracyBest;
//设置地图初始的缩放比例
[self.mapView setZoomLevel:15 animated:YES];
创建多边形围栏
- (void)getPolygonPointArray{
NSLog(@"%d ",self.pointArray.count);
CLLocationCoordinate2D coordinates[_pointArray.count];
for(inti=0; i<_pointArray.count; i++) {
AMapGeoPoint*coorModel =_pointArray[i];
coordinates[i].latitude= coorModel.latitude;
coordinates[i].longitude= coorModel.longitude;
}
MAPolygon*polygon = [MAPolygonpolygonWithCoordinates:coordinatescount:_pointArray.count];
[self.polygonsaddObject:polygon];
NSLog(@"%d",self.polygons);
[self.mapView addOverlays:self.polygons];
[self requestAddFence];
}
自定义大头针的样式
- (MAAnnotationView*)mapView:(MAMapView*)mapView viewForAnnotation:(id)annotation
{
if([annotationisKindOfClass:[MAPointAnnotationclass]])
{
staticNSString*pointReuseIndentifier =@"pointReuseIndentifier";
if (_isPolygonFence == NO && _isCircleFence == NO) {
CustomAnnotationView*annotationView = (CustomAnnotationView*)[mapViewdequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
if(annotationView ==nil)
{
annotationView = [[CustomAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:pointReuseIndentifier];
}
annotationView.portrait= [UIImageimageNamed:@"location"];
annotationView.canShowCallout=NO;//设置气泡可以弹出,默认为NO
returnannotationView;
}else{
PointFenceView*annotationView = (PointFenceView*)[mapViewdequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
if(annotationView ==nil)
{
annotationView = [[PointFenceViewalloc]initWithAnnotation:annotationreuseIdentifier:pointReuseIndentifier];
}
annotationView.canShowCallout=NO; //设置气泡可以弹出,默认为NO
returnannotationView;
}
}
return nil;
}