1
- (void)viewDidLoad
{
    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(foundTap:)];
    tapRecognizer.numberOfTapsRequired = 1;
    tapRecognizer.numberOfTouchesRequired = 1;
    [self.myMapView addGestureRecognizer:tapRecognizer];
}

-(void)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];
}

当我在 MKMapView 中选择一个位置时,我使用上面的代码在 MKMapView 上进行精确定位进步。

4

2 回答 2

1

尝试将传入的对象更改为locationInView:方法self.view

-(void)foundTap:(UITapGestureRecognizer *)recognizer
{
    CGPoint point = [recognizer locationInView:self.view];  

    CLLocationCoordinate2D tapPoint = [self.myMapView convertPoint:point toCoordinateFromView:self.view];

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

    point1.coordinate = tapPoint;

    [self.myMapView addAnnotation:point1];
}
于 2013-07-15T06:41:08.277 回答
1

我知道另一个答案对你有用,但我只是想展示正确的使用方法convertPoint:toCoordinateFromView:。与其传递容器视图,不如传递该点所在的地图视图:

CLLocationCoordinate2D tapPoint = [self.myMapView convertPoint:point
                                          toCoordinateFromView:self.myMapView];

这样可以节省视图之间的交换。这是完整的方法:

-(void)foundTap:(UITapGestureRecognizer *)recognizer
{
    CGPoint point = [recognizer locationInView:self.myMapView];  
    CLLocationCoordinate2D tapPoint = [self.myMapView convertPoint:point toCoordinateFromView:self.myMapView];
    MKPointAnnotation *point1 = [[MKPointAnnotation alloc] init];
    point1.coordinate = tapPoint;
    [self.myMapView addAnnotation:point1];
}
于 2013-07-15T09:44:10.480 回答