GMSMapView didTapAtCoordinate在平移地图之前给出错误的坐标

时间:2015-01-20 00:54:03

标签: ios google-maps uitableview uigesturerecognizer

我正在使用我作为自定义UITableViewCell添加的GMSMapView。当我第一次创建地图并捕获初始用户点击时,我的坐标基于用户在lat / lon 0/0附近点击 - 即使我的地图中心设置为43.50并且我得到的值为-1.800144 / -1.867676 -116.22(地图显示在这些坐标周围)。

然而,当我通过平移一次移动地图,然后点击它时,我的坐标更符合我的预期。另外,如果我将以下代码添加到GMSMapViewDelegate方法mapView:idleAtCameraPosition:我第一次得到正确的坐标:

    static dispatch_once_t onceToken ;
dispatch_once(&onceToken, ^{
    GMSCameraUpdate *update = [GMSCameraUpdate setCamera:position] ;
    [mapView moveCamera:update] ;
}) ;

但这似乎是一个隐藏更基本问题的黑客。

以下是相关代码:

我的自定义表格视图单元格在其构造函数中创建初始mapView:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Camera position is moot, just need something to instantiate the map view
        GMSCameraPosition * cameraPosition = [GMSCameraPosition cameraWithLatitude:0 longitude:0 zoom:5];

        GMSMapView * mapView = [GMSMapView mapWithFrame:CGRectZero camera:cameraPosition];

        _mapView = mapView ;
     }
    return self;
}

在我的视图控制器中,我设置了手势,设置了我的委托和我的地图边界:

- (void) configureMap
{
    [_mapView.settings setAllGesturesEnabled:YES];
    [_mapView.settings setConsumesGesturesInView:YES] ;

    _mapView.delegate = self ;

    // _userTrip has among other things, a bounds for our map
    GMSCoordinateBounds * bounds = _userTrip.bounds ;

    DDLogInfo(@"Trip Bounds are sw(%f:%f) ne(%f:%f)",   bounds.southWest.latitude, bounds.southWest.longitude, bounds.northEast.latitude, bounds.northEast.longitude) ;

    GMSCameraUpdate *cameraUpdate = [GMSCameraUpdate fitBounds:bounds];
    [_mapView moveCamera:cameraUpdate];
}

我的相机位置代表:

- (void) mapView:(GMSMapView *)mapView idleAtCameraPosition:(GMSCameraPosition *)position
{
    DDLogInfo(@"User changed map size. Zoom is now set to %f", mapView.camera.zoom) ;
    /* putting this in 'fixes' the problem
    static dispatch_once_t onceToken ;
    dispatch_once(&onceToken, ^{
        GMSCameraUpdate *update = [GMSCameraUpdate setCamera:position] ;
        [mapView moveCamera:update] ;
    }) ;
     */
 }

我的水龙头委托方法:

- (void) mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate
{
    DDLogInfo(@"User did tap at coordinate %f, %f", coordinate.latitude, coordinate.longitude) ;
    DDLogInfo(@"Map view center %f %f and zoom is %f",  mapView.camera.target.latitude, mapView.camera.target.longitude, mapView.camera.zoom) ;
}

示例输出:

[] Trip Bounds are sw(43.409890:-116.435511) ne(43.597180:-116.023860)

[] map view zoom = 10.053131

[] User changed map size. Zoom is now set to 10.053131

--- At this point, the map is displayed and it shows the right location based on the bounds in the log statement above. Then, when I tap on the map:

[] User did tap at coordinate -0.262352, -0.900879

[] Map view center 43.503608 -116.229685 and zoom is 10.053131

--- As you can see above, the user coordinates are way off based on where the map thinks it is. Below, I have panned the map and then did another tap

[] User changed map size. Zoom is now set to 10.053131

[] User did tap at coordinate 43.447926, -116.340871

[] Map view center 43.426753 -116.377271 and zoom is 10.053131

如您所见,用户坐标现在看起来是地图上的有效位置。我已经通过调试器验证了整个引用相同的映射实例并且它具有相同的gestureRecognizer。

我有一个相同代码的先前版本没有将GMSMapView放在表格视图单元格中,并且它没有出现同样的问题。因此,我怀疑将其置于UITableViewCell中会导致初始化问题。

任何人遇到类似这样的事情并且知道如何修复它?

0 个答案:

没有答案