Ios 绘图插针未指向MKMapView上的确切位置
我使用上面的代码在MKMapView中选择一个位置时在MKMapView上做了一个pin点。它没有指向我确切触摸的位置。我触摸的地方有点远。我的代码有什么问题吗?请提供任何帮助。请提前感谢。尝试将传入对象更改为Ios 绘图插针未指向MKMapView上的确切位置,ios,objective-c,mkmapview,uigesturerecognizer,Ios,Objective C,Mkmapview,Uigesturerecognizer,我使用上面的代码在MKMapView中选择一个位置时在MKMapView上做了一个pin点。它没有指向我确切触摸的位置。我触摸的地方有点远。我的代码有什么问题吗?请提供任何帮助。请提前感谢。尝试将传入对象更改为位置查看:方法self.view: - (void)viewDidLoad { UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@s
位置查看:
方法self.view
:
- (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];
}
我知道另一个答案适用于您,但我只是想展示如何正确使用
convertPoint:toCoordinateFromView:
。与其传递容器视图,不如传递点所在的地图视图:
-(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];
}
这样可以节省视图之间的交换。以下是完整的方法:
CLLocationCoordinate2D tapPoint = [self.myMapView convertPoint:point
toCoordinateFromView:self.myMapView];
谢谢你的回答。如何在MKMapView中获得选择位置的经度和纬度值?它们包含在
CLLocationCoordinate2D
中,在代码中的变量point1
中。如何获取在MKMapView中选择位置的经度和纬度值?在您的情况下,您可以执行:tapPoint.latitude
和tapPoint.longitude
或point1.coordinate.latitude
和point1.coordinate.longitude
。看见
-(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];
}