推荐上车点-iOS 最后更新时间: 2021年01月22日
概述
乘客端选择上车位置中, 通过集成接入推荐上车点SDK, 进行实现推荐上车点功能.
使用流程
- 定位确定相关位置, 设置地图展示层级, 若定位精度差, 推荐提示乘客确定上车点位置
- 通过推荐上车点功能获得推荐附近可用上车点数据
- 推荐上车点SDK进行吸附到吸附范围内上车点
- 没有可吸附上车点, 会返回附近POI名称
接入使用
1、创建MAHotSpotController
[MAHotSpotManager sharedInstance].accessKey = "your key";
self.hotSpotController = [[MAHotSpotController alloc] initWithMapView:self.mapView delagate:self];
2、配置MAHotSpotController
需要把mapview的delegate方法透传给MAHotSpotController
/**
* @brief 在对应的地图delegate方法中调用此方法
* @param mapView 地图
* @param wasUserAction 是否用户触发
*/
- (void)mapView:(MAMapView *)mapView mapWillMoveByUser:(BOOL)wasUserAction {
[self.hotSpotController mapView:mapView mapWillMoveByUser:wasUserAction];
}
/**
* @brief 在对应的地图delegate方法中调用此方法
* @param mapView 地图
* @param wasUserAction 是否用户触发
*/
- (void)mapView:(MAMapView *)mapView mapDidMoveByUser:(BOOL)wasUserAction {
[self.hotSpotController mapView:mapView mapDidMoveByUser:wasUserAction];
}
/**
* @brief 在对应的地图delegate方法中调用此方法
* @param mapView 地图
* @param wasUserAction 是否用户触发
*/
- (void)mapView:(MAMapView *)mapView mapDidZoomByUser:(BOOL)wasUserAction {
[self.hotSpotController mapView:mapView mapDidZoomByUser:wasUserAction];
}
/**
* @brief 在对应的地图delegate方法中调用此方法
* @param mapView 地图
* @param views annotationView
*/
- (void)mapView:(MAMapView *)mapView didAddAnnotationViews:(NSArray *)views {
[self.hotSpotController mapView:mapView didAddAnnotationViews:views];
}
/**
* @brief 在对应的地图delegate方法中调用此方法
* @param mapView 地图
* @param annotation 标注
* @return 返回上车点功能需要的annotationview,一类是上车点,一类是大头针锚点
*/
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation {
MAAnnotationView *ret = [self.hotSpotController mapView:mapView viewForAnnotation:annotation];
if(ret) {
return ret;
} else {
//do your job here
}
return nil;
}
3、上车点和大头针的显示与自定义
///创建上车点view
- (MAAnnotationView *)createHotSpotViewWith:(MAHotSpotAnnotation *)annotation reuseIdentifier:(NSString *)reuseIdentifier controller:(MAHotSpotController *)controller {
MAHotSpotAnnotationView *ret = [[MAHotSpotAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
ret.animatingCircleImage = [UIImage imageNamed:@"spotCircle"];
ret.centerDotImage = [UIImage imageNamed:@"centerDot"];
return ret;
}
///创建大头针锚点view
- (MAAnnotationView *)createAnchorPinViewWith:(MAPointAnnotation *)annotation reuseIdentifier:(NSString *)reuseIdentifier controller:(MAHotSpotController *)controller {
MAPinAnnotationView *view = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
view.canShowCallout = YES;
view.pinColor = MAPinAnnotationColorGreen;
return view;
}
3、不通过MAHotSpotController,直接使用接口查询附近上车点并展示
@interface MAHotSpotManager : NSObject
///服务授权的key
@property (nonatomic, copy) NSString *accessKey;
/**
* @brief 单例方法
* @return 单例对象
*/
+ (instancetype)sharedInstance;
/**
* @brief 查询周围上车点
* @param request 请求
* @param callback 结果回调
* @return NSOperation 对象
*/
- (NSOperation *)queryHotSpotsWith:(MAHotSpotQueryRequest *)request callback:(void(^)(MAHotSpotQueryResponse *response, NSError* error))callback;
@end
4、自定义MAHotSpotController其他行为
///如果返回NO,preCoord为上次请求的中心点坐标,absorbedAnno返回指定要自动吸附到的上车点,clearPrevAnnos返回是否删除上次的上车点。如不实现则执行默认策略:zoomLevel<15:不请求,移动超过1000米清空上车点;zoomLevel>=15: 锚点和已有上车点屏幕距离50像素内,自动吸附且不请求新的,否则不清空原来的且请求新的。
- (BOOL)shouldSendSpotQueryRequest:(CLLocationCoordinate2D)centerCoord
preCoord:(CLLocationCoordinate2D)preCoord
absorbedAnno:(MAHotSpotAnnotation **)absorbedAnno
clearPrevAnnos:(BOOL*)clearPrevAnnos
controller:(MAHotSpotController *)controller;
///请求返回后,判断要不要自动吸附到最近上车点, 如不实现则执行默认策略:如果地图zoomLevel小于15则返回NO,zoomLevel不小于15且地图中心点距离上车点距离小于50屏幕坐标返回YES。
- (BOOL)shouldAutoAbsorbedToAnnotation:(MAHotSpotAnnotation *)anno controller:(MAHotSpotController *)controller;
///如不实现此代理方法,当做返回YES处理
- (BOOL)shouldSendRegeoQueryRequest:(CLLocationCoordinate2D)centerCoord controller:(MAHotSpotController *)controller;
///如不实现,当做返回formattedAddress处理
- (NSString *)regeoDisplayStringWith:(AMapReGeocodeSearchResponse *)response controller:(MAHotSpotController *)controller;
///大头针锚点跳动动画回调, 如不实现则走默认动画效果
- (void)startAnchorPinViewAnimating:(MAAnnotationView *)anchorPinView controller:(MAHotSpotController *)controller;
5、MAHotSpotController错误监听
///错误回调, 如果errorcode==MAHotSpotErrorSpots时为请求上车点错误,其他为逆地理查询错误
- (void)hotSpotController:(MAHotSpotController *)controller didFailWithError:(NSError *)error;
高级功能
功能1:设置上车点显示级别
///上车点允许获取和显示的最小地图缩放级别,默认15
@property (nonatomic, assign) CGFloat minMapZoomLevel;
功能2: 设置请求推荐上⻋点数量
///返回上车点个数,最大支持3个
@property (nonatomic, assign) int spotSearchCount;
功能3: 自定义上车点数据源
///如果实现此方法,内部将不请求附近上车点,改由外部通过回调返回上车点信息
- (void)queryHotSpotsWith:(CLLocationCoordinate2D)centerCoord
sucCallback:(void(^)(NSArray<MAHotSpotInfo*> *resultSpots))sucCallback
failCallback:(void(^)(BOOL isCancelled, NSString* errDescription))failCallback
controller:(MAHotSpotController *)controller;
/**
*@brief 外部传入上车点信息做更新,适用于外部自己控制上车点获取
*/
- (void)updateHotSpotsWith:(NSArray<MAHotSpotInfo*> *)spots;
注意事项
- 定位精度差的情况下, 推荐提示乘客二次确认上车点位置
- 自有上车点情况下, 支持使用自有数据源展示和吸附