Ios MKMapView-如何检测缩放与拖动事件

Ios MKMapView-如何检测缩放与拖动事件,ios,objective-c,mkmapview,Ios,Objective C,Mkmapview,如何在MKMapView上检测缩放与拖动事件 我想重新加载地图,以防用户想要将地图拖动/滚动到新位置。我不想使用放大/缩小事件重新加载地图。您可以通过下面的PangestureRecognitor方法找到 - (void)viewDidLoad { UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragMap:)]; p

如何在MKMapView上检测缩放与拖动事件


我想重新加载地图,以防用户想要将地图拖动/滚动到新位置。我不想使用放大/缩小事件重新加载地图。

您可以通过下面的PangestureRecognitor方法找到

- (void)viewDidLoad
{
   UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragMap:)];
   panGesture.delegate = self;
   [mapView addGestureRecognizer:panGesture];
}

-(void)dragMap:(UIPanGestureRecognizer*) gestureRecognizer
{
   if(gestureRecognizer.state == UIGestureRecognizerStateBegan)
   {
      NSLog(@"The mapView Dragged");
   }
 }

我理解这个问题

缩放或拖动两种情况调用
regionWillChangeAnimated
regionDidChangeAnimated
以区分缩放和拖动

MapViewControllerDelegate中

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated 
{
    // check mapView.centerCoordinate
    // if mapView.centerCoordinate is equal previousCenterCoordinate {
        // ZOOM
    // } else {
        //pinch gesture
    //}
}
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated 
{
    //  zoomLevel = [mapView getZoomLevel]; // or [self getZoomLevel] in your case
    // if zoomLevel isEqual previousZoomLevel {
        // did drag
    // } else {
        // did zoom
        // previousZoomLevel = zoomLevel
    //}
}

更新:

我想我前面的例子会有用的。但事实并非如此。这是我的第二次尝试

添加一个方法
getZoomLevel

#define MERCATOR_OFFSET 268435456
#define MERCATOR_RADIUS 85445659.44705395

- (double) getZoomLevel
{
    return 20.00 - log2(self.region.span.longitudeDelta * MERCATOR_RADIUS * M_PI / (180.0 * self.bounds.size.width));
}
在该方法中,
self
mapView
。我有一个
MKMapView
类别,因此在我的例子中它是
self

MapViewControllerDelegate中

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated 
{
    // check mapView.centerCoordinate
    // if mapView.centerCoordinate is equal previousCenterCoordinate {
        // ZOOM
    // } else {
        //pinch gesture
    //}
}
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated 
{
    //  zoomLevel = [mapView getZoomLevel]; // or [self getZoomLevel] in your case
    // if zoomLevel isEqual previousZoomLevel {
        // did drag
    // } else {
        // did zoom
        // previousZoomLevel = zoomLevel
    //}
}
我找到了一个解决办法:(。真的很简单 1.保持上一个缩放级别。 2.在regionDidChangeAnimated方法中,获取地图的新缩放级别,并与上一级别进行检查。 这是我的密码

#定义墨卡托半径85445659.44705395
#定义kVerySmalValue(0.000001)
-(BOOL)compare2Double:(double)第一等位到:(double)第二等位{
if(晶圆厂(第一秒)
这没有帮助。regionDidChangeAnimated方法同时触发缩放和拖动。我只想捕获拖动事件。Robert TuanVu我更新了我的编码。请检查。我以前使用过该代码:)没什么。它始终检测到drap和zoommapView.centerCoordinate=>change@RobertTuanVu答案已更新。