Can't Get Coordinates From MKMapView Using UILongTouchGestureRecognizer

ぐ巨炮叔叔 提交于 2020-01-05 07:38:16

问题


I'm using iOS 6, and I've read dozens of ways to accomplish this (including many from stack overflow) without success.

Here's what I've tried, separated into "stages":

  1. Create a UILongTouchGestureRecognizer to receive long touches for the MKMapView.

    • I've tried adding a UILongTouchGestureRecognizer via my Storyboard, and hooking up the outlets, delegates, etc. through the Connections Inspector.
    • I've tried creating the UILongTouchGestureRecognizer programmatically, initializing it using mapView as target, self as target, and self.view as target.
  2. Receive the touch gesture using the selector method from the UILongTouchGestureRecognizer, get the CGPoint, and convert it to a CLLocationCoordinate2D object.

    • I've tried:
      • Using [mapView convertPoint:(CGPoint) toCoordinateFromView:self.mapView];
      • Using MKMapPoint aPoint = MKMapPointMake(aCGPoint.x, aCGPoint.y); first, and then using MKCoordinateForMapPoint(aMapPoint) to get the CLLocationCoordinate2D.
      • Both Directly accessing the UILongPressGestureRecognizer and using the (UILongPressGestureRecognizer *)sender from the method call to get the x,y for the CGRect.
  3. Results

    • In a nutshell here is what I've been getting when checking the values using NSLog, irregardless of the methods used. - For the X,Y given from the UILongPressGestureRecognizer, x seems to fluctuate to somewhere in the range of 200, and 420. Y ranges from ~400 - 700. - The latitudes and longitudes triggered by a long touch & printed to the log, strangely, only vary in the 3rd - 6th decimal values after the decimal point of 85.051XX (latitude) and -179.9997XX (longitude).

Here's examples of some of the code I've tried

- (void)viewDidLoad
{
     NSLog(@"View did load");
    [super viewDidLoad];
    UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:mapView action:@selector(handleLongPress:)];
    mapView = [[MKMapView alloc] init];
}

- (IBAction)handleLongPress:(UILongPressGestureRecognizer *)sender
{
    NSLog(@"CGPoint point: x - %f y - %f", point.x, point.y);
    CGPoint point = [sender locationInView:self.mapView];
    CLLocationCoordinate2D locCoord = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
    NSLog(@"Coords from \"locCoord\": lat - %f lng - %f", locCoord.latitude, locCoord.lon);

    MKPointAnnotation *addAnnotation = [[MKPointAnnotation alloc] init];

   [addAnnotation setCoordinate:
   [self.mapView addAnnotation:addAnnotation];
}

Hope someone can help me, because I'm completely stumped on this one.

Just to reiterate, the end goal is to get a coordinate of where the user "long pressed a mapView", and then (something else I can't get to work) drop a pin at that location.


回答1:


Looking back at an old project of mine that works in iOS 6 we seem to have almost identical code (same source tutorial I guess). The difference being that I am checking the state and that my mapView variable is not being reset in the viewDidLoad. You seem to be allocating a new one after the gesture recogniser and you haven't shown any code for adding it to your viewController. I'd guess that you've managed to separate the one on screen from the one in code. If your MKMapview is meant to be on screen through the life of the viewcontroller, let IB manage the allocation and assignment.

- (IBAction)handleLongPress:(UILongPressGestureRecognizer *)recognizer
{
    if (recognizer.state == UIGestureRecognizerStateBegan)
    {
        CGPoint point = [recognizer locationInView:mapView];
        CLLocationCoordinate2D locCoord = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
        [self addPinAtLocation:locCoord];       
    }

}


来源:https://stackoverflow.com/questions/13336650/cant-get-coordinates-from-mkmapview-using-uilongtouchgesturerecognizer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!