CGPoint到CLLLocationCoordinate2D - convertPoint toCoordinateFromView无法正常工作

时间:2012-06-28 20:39:57

标签: objective-c

我有几个月前写过的一个映射项目,我使用了一个帮助类,它将触摸参数传递给我的主mapview类。使用以下方法,我通常可以将CGPoint x和y信息转换为地理坐标:

- (void)moveDetect:(NSSet *)touches :(UIEvent *)event {

UITouch *touch = [[event allTouches] anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
NSLog(@"X:%f  Y:%f", touchPoint.x, touchPoint.y);

CLLocationCoordinate2D touchCoordinate = [self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];

float lat = touchCoordinate.latitude;
float lng = touchCoordinate.longitude;
NSLog(@"Touch Coordinate: %f %f", lat, lng);}

我最近尝试在不同的项目中重用代码,但转换不会发生。暂停和步入touchCoordinate变量最初显示值(latitude = {CLLocationDegrees} 3.23141e-306和longitude = {CLLocationDegrees} 5.46786e-48),但是一旦到达下一行代码,值就都是0.我已经审查了这两个项目,但一直无法弄清楚为什么一个有效,而另一个没有。我很感激时间,并期待任何可以引导我朝着正确方向前进的建议。感谢。

1 个答案:

答案 0 :(得分:0)

使用此:

  - (void)moveDetect:(NSSet *)touches :(UIEvent *)event {

    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchPoint = [touch locationInView:self.mapView]; //here locationInView it would be mapView
    NSLog(@"X:%f  Y:%f", touchPoint.x, touchPoint.y);

    CLLocationCoordinate2D touchCoordinate = [self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];
 }