如何确定用户是否将地图MKMapView移动了一定的百分比?

时间:2014-03-20 19:10:09

标签: ios mkmapview cllocationdistance

我想确定用户是否将地图移动了一定百分比(比方说20%)。我怎么能实现这个目标?运动可以在任何方向。

1 个答案:

答案 0 :(得分:2)

这是一个想法:

步骤1:声明坐标属性

@property CLLocationCoordinate2D lastCoordinate;

第2步:在地图启动时,运行:

_lastCoordinate = [map convertPoint:self.view.center toCoordinateFromView:yourMap];

第3步:监控

- (void) mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    CGPoint currentPoint = [mapView convertCoordinate:_lastCoordinate toPointToView:self.view];

    int xDistance = currentPoint.x - self.view.center.x;
    if (xDistance < 0) xDistance = xDistance * -1;

    if (xDistance > (self.view.bounds.size.width / 5)) {
        // moved 20% on x axis
        _lastCoordinate = [mapView convertPoint:self.view.center toCoordinateFromView:self.view];
    }
    else {
        int yDistance = currentPoint.y - self.view.center.y;
        if (yDistance < 0) yDistance = yDistance * -1;

        if (yDistance > (self.view.bounds.size.height / 5)) {
            // moved 20% on y axis
            _lastCoordinate = [mapView convertPoint:self.view.center 
                               toCoordinateFromView:self.view];
        }
    }
}

我确信实施可能是一个触摸清洁工,但为了让你开始,我认为这应该指向你正确的方向。让我知道它是如何工作的!