在项目中遇到一个问题,在拖动或者缩放百度地图的时候要请求数据。但是百度地图SDK中没有明确如何获取拖动和缩放手势
官方推荐使用如下两个方法,通过判断状态来获取,但是也没有明确怎么判断。还有个问题就是如果在regionDidChangeAnimated请求数据的话,产品还有个需百度地图的中心点以最新一条数据的经纬度移动。只要中心点移动了又会调用regionDidChangeAnimated,这样就会造成多次请求接口。
/**
*地图区域即将改变时会调用此接口
*@param mapView 地图View
*@param animated 是否动画
*/
- (void)mapView:(BMKMapView *)mapView regionWillChangeAnimated:(BOOL)animated;
/**
*地图区域改变完成后会调用此接口
*@param mapView 地图View
*@param animated 是否动画
*/
- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated;
这里推荐使用自定义手势来判断.
注意:加自定义手势时,必须设置UIGestureRecognizer的属性cancelsTouchesInView 和 delaysTouchesEnded 为NO,否则影响地图内部的手势处理。
// 拖动
UIPanGestureRecognizer *mapPanGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(mapPanGesture:)];
mapPanGesture.delegate = self;
mapPanGesture.cancelsTouchesInView = NO;
mapPanGesture.delaysTouchesEnded = NO;
[_mapView addGestureRecognizer:mapPanGesture];
// 缩放
UIPinchGestureRecognizer *mapPinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(mapPinchGesture:)];
mapPinchGesture.delegate = self;
mapPinchGesture.cancelsTouchesInView = NO;
mapPinchGesture.delaysTouchesEnded = NO;
[_mapView addGestureRecognizer:mapPinchGesture];
/**
百度地图拖动手势
@param gesture 手势
*/
- (void)mapPanGesture:(UIGestureRecognizer *)gesture
{
if ([gesture state] == UIGestureRecognizerStateBegan) {
}
}
/**
百度地图缩放手势
@param gesture 手势
*/
- (void)mapPinchGesture:(UIGestureRecognizer *)gesture
{
if ([gesture state] == UIGestureRecognizerStateBegan) {
}
}
网友评论