检测用户何时滚动MKMapView一定距离?

Ala*_*lan 6 iphone mapkit

我想确定用户是否滚动了超过地图的某个百分比,然后禁用地图从用户位置居中(类似于地图应用的工作方式).

我不确定使用哪种方法.

我认为创建一个矩形并查看矩形是否包含当前中心点是很简单的,但是我必须以IOS 3为目标,因此我无法使用许多较新的Mapkit apis.

我已尝试使用CLLocation,并在当前mapcenter和用户位置之间使用distanceFrom,但我试图弄清楚该距离是否为某个百分比.

Mic*_*eed 15

我个人觉得有人可以发布一段代码与一般散文有关如何解决这个问题更有帮助.以下是我提出的内容 - 大致已被黑客攻击以更好地回答这个问题:

在头文件中,我有:

#define SCROLL_UPDATE_DISTANCE          80.00
Run Code Online (Sandbox Code Playgroud)

在我看来(这是CLLocationManagerDelegate的委托,MKMapViewDelegate):

// this method is called when the map region changes as a delegate of MKMapViewDelegate
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
    NSLog(@"regionDidChangeAnimated");
    MKCoordinateRegion mapRegion;   
    // set the center of the map region to the now updated map view center
    mapRegion.center = mapView.centerCoordinate;

    mapRegion.span.latitudeDelta = 0.3; // you likely don't need these... just kinda hacked this out
    mapRegion.span.longitudeDelta = 0.3;

    // get the lat & lng of the map region
    double lat = mapRegion.center.latitude;
    double lng = mapRegion.center.longitude;

    // note: I have a variable I have saved called lastLocationCoordinate. It is of type
    // CLLocationCoordinate2D and I initially set it in the didUpdateUserLocation
    // delegate method. I also update it again when this function is called
    // so I always have the last mapRegion center point to compare the present one with 
    CLLocation *before = [[CLLocation alloc] initWithLatitude:lastLocationCoordinate.latitude longitude:lastLocationCoordinate.longitude];
    CLLocation *now = [[CLLocation alloc] initWithLatitude:lat longitude:lng];

    CLLocationDistance distance = ([before distanceFromLocation:now]) * 0.000621371192;
    [before release];
    [now release];

    NSLog(@"Scrolled distance: %@", [NSString stringWithFormat:@"%.02f", distance]);

    if( distance > SCROLL_UPDATE_DISTANCE )
    {
        // do something awesome
    }

    // resave the last location center for the next map move event
    lastLocationCoordinate.latitude = mapRegion.center.latitude;
    lastLocationCoordinate.longitude = mapRegion.center.longitude;

}
Run Code Online (Sandbox Code Playgroud)

希望能为您提供正确的方向.

distanceFromLocation是iOS 3.2及更高版本.initWithLatitude是iOS 2.0及更高版本.MKCoordinateRegion是iOS 3.0及更高版本.MKMapView centerCoordinate是iOS 3.0及更高版本.

另外 - 请随意跳进去,让我直接在我错误的地方.我自己也在考虑所有这些 - 但到目前为止,这对我来说相当不错.

希望这有助于某人.