1.公司的项目是面向房地产的,包含新房(全国多个分公司),存租房(与建行建方的合作)项目,所以地图与看房相结合比较多,之前为了进度,地图这一块的业务都是H5实现,现在稳定下来后,H5的体验度不是很高,所以地图这一块全部换成原生。这篇文章也是做个探索和总结,以便大家一起参考学习。
2.地图采用百度地图,主要功能包括:定位+替换大头针图标,两点连线及采用自定义图片样式,导航,点标注,自定义标注UI,点击事件,点聚合的算法(重点)+优化
一. 定位+替换大头针图标
#pragma mark - 定位当前位置
- (void)initLocation
{
///BMKLocationManager类。初始化之前请设置 BMKLocationAuth 中的APIKey,否则将无法正常使用服务.
_locationManager = [[BMKLocationManager alloc] init];
_locationManager.delegate = self;
_locationManager.coordinateType = BMKLocationCoordinateTypeBMK09LL;
_locationManager.distanceFilter = kCLDistanceFilterNone;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager.activityType = CLActivityTypeAutomotiveNavigation;
_locationManager.pausesLocationUpdatesAutomatically = NO;
_locationManager.allowsBackgroundLocationUpdates = YES;
_locationManager.locationTimeout = 10;
_locationManager.reGeocodeTimeout = 10;
__weak BMKClusterAnnotationPage *weakSelf = self;
///单次定位。如果当前正在连续定位,调用此方法将会失败,返回NO。\n该方法将会根据设定的 desiredAccuracy 去获取定位信息。如果获取的定位信息精确度低于 desiredAccuracy ,将会持续的等待定位信息,直到超时后通过completionBlock返回精度最高的定位信息。\n可以通过 stopUpdatingLocation 方法去取消正在进行的单次定位请求
[_locationManager requestLocationWithReGeocode:YES withNetworkState:YES completionBlock:^(BMKLocation * _Nullable location, BMKLocationNetworkState state, NSError * _Nullable error) {
BMKClusterAnnotationPage *strongSelf = weakSelf;
MyLocation * loc = [[MyLocation alloc]initWithLocation:location.location withHeading:nil];
[strongSelf addLocToMapView:loc];
}];
//替换大头针图片方法之一
BMKLocationViewDisplayParam *displayParam = [[BMKLocationViewDisplayParam alloc] init];
displayParam.locationViewOffsetX=0;//定位偏移量(经度)
displayParam.locationViewOffsetY=0;//定位偏移量(纬度)
displayParam.isAccuracyCircleShow=NO;//经度圈是否显示
//这里替换自己的图标路径,必须把图片放到百度地图SDK的Resources/mapapi.bundle/images 下面
//还有一种方法就是获取到_locationView之后直接设置图片
displayParam.locationViewImgName=@"pin_red";
[_mapView updateLocationViewWithParam:displayParam];
}
- (void)addLocToMapView:(MyLocation *)loc
{
[_mapView updateLocationData:loc];
[_mapView setCenterCoordinate:loc.location.coordinate animated:YES];
[_mapView setZoomLevel:18];
_mapView.showsUserLocation = YES; //一定要设置这个参数为YES,不然大头针不出来
}
二.两点连线及采用自定义图片样式
#pragma mark - 两点之间划线
- (void)lineTwoAction:(MyLocation*)myLoc andOther:(MyLocation*)otherLoaction{
CLLocationCoordinate2D coors[2] = {0};
coors[1].latitude = myLoc.location.coordinate.latitude;
coors[1].longitude = myLoc.location.coordinate.longitude;//定位坐标1
coors[0].latitude = otherLoaction.location.coordinate.latitude;
coors[0].longitude =