7

I'm trying to catch a coordinate from touch event. I can catch but when give zoom have big numbers and when I put this in a function

newCoord = [mapView convertPoint:location toCoordinateFromView:mapView],

I get wrong coordinates. What can I do?

My code:

UITouch *touch = [touches anyObject];

CGPoint location = [touch locationInView:touch.view];


NSLog(@"locationTOUCH:%f,%f", location.x,location.y);

CLLocationCoordinate2D newCoord;

newCoord = [mapView convertPoint:location toCoordinateFromView:mapView];

NSLog(@"coordinate-%f,%f", newCoord.latitude,newCoord.longitude); 
Ortwin Gentz
  • 52,648
  • 24
  • 135
  • 213
vijay adhikari
  • 2,455
  • 1
  • 16
  • 22
  • 1
    Is touch.view the same as mapView? It's probably better to use a UIGestureRecognizer to detect touches on the map. See [this answer](http://stackoverflow.com/questions/4317810/how-to-capture-tap-gesture-on-mkmapview/4318835#4318835). That answer doesn't mention you should also return YES in shouldRecognizeSimultaneouslyWithGestureRecognizer to avoid interfering with the map's gesture recognizers. –  Jul 28 '11 at 12:39

1 Answers1

1

I'd guess it should be fixed by changing

CGPoint location = [touch locationInView:touch.view];

to

CGPoint location = [touch locationInView:mapView];
Ortwin Gentz
  • 52,648
  • 24
  • 135
  • 213
  • Ortwin is right : the views' coordinate systems are probably not the same, that's why you get wrong coordinates using one for touch detection and the other for coordinates conversion. – psycho Sep 07 '11 at 21:41