Plot引脚未指向MKMapView上的确切位置

时间:2013-07-15 06:18:28

标签: ios objective-c mkmapview uigesturerecognizer

- (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中选择一个位置时,它并没有指向我接触到的地方。我接触的地方很远。我的代码出了什么问题?。任何帮助将不胜感激提前谢谢。

2 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分: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];
}