2013-07-15 46 views
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的地方它不地步,我正好touches.It遠遠有點其中i touches.What我的代碼錯了嗎?。任何幫助將不勝感激。提前感謝。情節銷不上的MKMapView

回答

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]; 
} 
+0

感謝UR answer.it工作正常 – Xcode

+0

我如何獲得的MKMapView選擇位置的經度和緯度值? – Xcode

+0

在你的情況下,你會做:tapPoint.latitude和tapPoint.longitude或point1.coordinate.latitude和point1.coordinate.longitude。請參閱http://developer.apple.com/library/mac/#documentation/CoreLocation/Reference/CoreLocationDataTypesRef/Reference/reference.html – Stunner

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]; 
} 
+0

感謝您的回答。我如何獲取經度和緯度值在MKMapView中選擇位置? – Xcode

+0

它們包含在代碼中變量'point1'的'CLLocationCoordinate2D'中。 –