简体   繁体   English

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

[英]Detecting when a user scrolls MKMapView a certain distance?

I want to determine if a user has scrolled more than a certain percentage of the map then disable centering of the map from the user location (similar to how the Maps app works). 我想确定用户是否滚动了超过地图的某个百分比,然后禁用地图从用户位置居中(类似于地图应用的工作方式)。

I'm not sure which methods to make use of. 我不确定使用哪种方法。

I think it would be straightforward to create a rectangle and see if the rectangle contains the current center point, however I have to target IOS 3, so I can't make use of many of the newer Mapkit apis. 我认为创建一个矩形并查看矩形是否包含当前中心点是很简单的,但是我必须以IOS 3为目标,因此我无法使用许多较新的Mapkit apis。

I've tried futzing with CLLocation, and using distanceFrom, between the current mapcenter, and the users location, but I'm trying to figure out if that distance is a certain percentage. 我已尝试使用CLLocation,并在当前mapcenter和用户位置之间使用distanceFrom,但我试图弄清楚该距离是否为某个百分比。

I personally find it more helpful when someone can post a snippet of code versus general prose about how one might go about this. 我个人觉得有人可以发布一段代码与一般散文有关如何解决这个问题更有帮助。 Here's what I came up with- roughly hacked out to simply better answer this question: 以下是我提出的内容 - 大致已被黑客攻击以更好地回答这个问题:

In a header file I have: 在头文件中,我有:

#define SCROLL_UPDATE_DISTANCE          80.00

and in my view (that is both a delegate for CLLocationManagerDelegate, MKMapViewDelegate): 在我看来(这是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;

}

Hope that sends you in the right direction. 希望能为您提供正确的方向。

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

Also- please feel free to jump in and set me straight where I've erred. 另外 - 请随意跳进去,让我直接在我错误的地方。 I'm figuring all of this out myself- but this is working fairly well for me so far. 我自己也在考虑所有这些 - 但到目前为止,这对我来说相当不错。

Hope this helps someone. 希望这有助于某人。

First lesson: Don't ask questions late night on SO. 第一课:不要在深夜问问题。

Second lesson: you can achieve this simply by construction a CGPoint from the user's current location, and a CGPoint from the MapView center. 第二课:您可以通过从用户的当前位置构建CGPoint和从MapView中心构建CGPoint来实现此目的。

With two points, just calculate the distance, and see if it's past a certain threshold. 有两个点,只需计算距离,看它是否超过某个阈值。

You can also construct a CGRect around the map center, and check CGRectContainsPoint if that's easier. 您还可以在地图中心周围构建CGRect,如果更容易,请检查CGRectContainsPoint。

- (BOOL) isUserPointInsideMapCenterRegion
{
    CLLocation * ul = _mapView.userLocation.location;

    CGPoint userPoint = [_mapView convertCoordinate: ul.coordinate toPointToView: _mapView];
    CGPoint mapPoint = [_mapView convertCoordinate: _mapView.centerCoordinate toPointToView: _mapView];

    if (fabs(userPoint.x - mapPoint.x) > MAP_CENTER_RECTANGLE_SIZE || fabs(userPoint.y - mapPoint.y) > MAP_CENTER_RECTANGLE_SIZE)
    {
        return NO;
    }

    return YES;
}

I realise this question is a bit old now, but I feel that the answer described in this other question is more robust because the delegate method could be fired for any reason. 我意识到这个问题是有点老了,但我觉得在描述的回答这个问题,其他的是更强大的,因为委托方法可以为任何原因被解雇。 Using a UIPanGestureRecognizer to detect the scroll means that the user manually scrolled the map, and it can check if the map has scrolled X pixels, instead of relying on meters, which means the user has scrolled more or less depending on the zoom level. 使用UIPanGestureRecognizer来检测滚动意味着用户手动滚动地图,并且它可以检查地图是否滚动了X像素,而不是依赖于米,这意味着用户根据缩放级别或多或少地滚动。

https://stackoverflow.com/a/11675587/159758 https://stackoverflow.com/a/11675587/159758

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当用户滚动MKMapView时,从Core Data中加载其他对象 - Load additional objects from Core Data when a user scrolls an MKMapView 使用MKMapView时如何设置精度和距离滤镜 - How to set accuracy and distance filter when using MKMapView 将MKMapView上的距离转换为UIView的距离 - Convert distance on MKMapView to distance for UIView 在 SwiftUI 应用程序中按下按钮时滚动到特定行的列表 - List that scrolls to certain row when button pressed in SwiftUI app 检测UIScroll在特定点何时查看并执行操作 - Detecting when a UIScrollView it at a certain point and perform an action 如何判断MKMapView上的regionChange事件是编程还是用户拖动MKMapView? - How can I tell when regionChange event on MKMapView is programatic or user-drag of MKMapView? MKMapView:显示用户当前位置时,引脚会更改颜色 - MKMapView: Pins Color Change When User Current Location is Shown 当用户向下滚动时,将UIView移到UITableView上方 - Move UIView above UITableView when user Scrolls Down 当用户滚动 UITableView 时,从 web 服务加载更多数据 - Load more data from a web service when the user scrolls the UITableView 用户在MKMapView上创建一个框 - User creating a box on MKMapView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM