【问题标题】:Detect touch nearby to CLLocationCoordinate2D in pixels检测到 CLLocationCoordinate2D 附近的触摸(以像素为单位)
【发布时间】:2015-03-18 14:16:46
【问题描述】:

我在GMSMapView 上有几个GMSMarker,它们都是可拖动的,所以当我长按它们时,我可以在地图上移动它们。但是,我在 GMSMapView 上也有一个长按操作,它添加了一个标记。

- (void)mapView:(GMSMapView *)mapView didBeginDraggingMarker:(GMSMarker *)marker {
    self.moving = YES;
}
- (void)mapView:(GMSMapView *)mapView didEndDraggingMarker:(GMSMarker *)marker {
    self.moving = NO;
}
- (void)mapView:(GMSMapView *)mapView didLongPressAtCoordinate:(CLLocationCoordinate2D)coordinate {
    if (self.moving) {
        return;
    }
    [self addMarkerAtCoordinate:coordinate];
}

现在的问题是,有时用户会误按而不是移动标记,而是添加一个新标记。因此,我想在标记周围添加小区域,用户无法添加新标记。我想过这样的事情:

- (void)mapView:(GMSMapView *)mapView didLongPressAtCoordinate:(CLLocationCoordinate2D)coordinate {
    CGFloat zoomFactor = 35.f - self.mapView.camera.zoom;
    CLLocation *location = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
    for (GMSMarker *marker in self.markers) {
        CLLocation *sectorLocation = [[CLLocation alloc] initWithLatitude:marker.position.latitude longitude:marker.position.longitude];
        if ([location distanceFromLocation:sectorLocation] < zoomFactor) {
            return;
        }
    }
}

但我当然不喜欢这种解决方案,因为该区域会随着缩放的变化而变化。我希望像标记周围的手指宽度这样的东西被长按禁止。这个距离怎么计算?

【问题讨论】:

    标签: ios objective-c google-maps google-maps-api-3


    【解决方案1】:

    GMSProjection GMSMapView 对象上使用pointForCoordinate: 方法很容易将坐标转换为视图上的位置。

    - (void)mapView:(GMSMapView *)mapView didLongPressAtCoordinate:(CLLocationCoordinate2D)coordinate {
        CGPoint longpressPoint = [mapView.projection pointForCoordinate:coordinate];
        for (GMSMarker *marker in self.markers) {
            CLLocationCoordinate2D markerCoordinate = marker.position;
            CGPoint sectorPoint = [mapView.projection pointForCoordinate:markerCoordinate];
            if (fabsf(longpressPoint.x - markerCoordinate.x) < 30.f && fabsf(longpressPoint.y - markerCoordinate.y) < 30.f) { // 30.f ofc should be defined as constant
                // handle situation when touchpoint is too close to marker
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-25
      • 2013-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多