检测CLLocationCoordinate2D附近的触摸(以像素为单位)

时间:2015-01-20 11:11:30

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

GMSMarker上的GMSMapView很少,所有这些都是可拖动的,所以当我长按它们时,我可以在地图上移动它们。不过我在GMSMapView上也有一个longpress操作,它添加了一个标记。

- (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;
        }
    }
}

但我当然不喜欢这个解决方案,因为区域随着变焦而变化。我想像标记周围的手指宽度被禁止长期禁止。如何计算这个距离?

1 个答案:

答案 0 :(得分:0)

使用pointForCoordinate: GMSProjection对象上的方法GMSMapView,可以轻松地将坐标转换为视图中的位置。

- (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
        }
    }
}