Get the coordinates of a point from mkmapview on iphone

前端 未结 5 1440
夕颜
夕颜 2020-12-08 22:36

I\'m trying to figure out how to put an annotation on a map based on where the user touches.

I have tried sub-classing the MKMapView and looked for the

5条回答
  •  生来不讨喜
    2020-12-08 23:05

    You can try this code

    - (void)viewDidLoad
    {
        UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(foundTap:)];
    
        tapRecognizer.numberOfTapsRequired = 1;
    
        tapRecognizer.numberOfTouchesRequired = 1;
    
        [self.myMapView addGestureRecognizer:tapRecognizer];
    }
    
    
    -(IBAction)foundTap:(UITapGestureRecognizer *)recognizer
    {
        CGPoint point = [recognizer locationInView:self.myMapView];  
    
        CLLocationCoordinate2D tapPoint = [self.myMapView convertPoint:point toCoordinateFromView:self.view];
    
        MKPointAnnotation *point1 = [[MKPointAnnotation alloc] init];
    
        point1.coordinate = tapPoint;
    
        [self.myMapView addAnnotation:point1];
    }
    

    All the best.

提交回复
热议问题