以像素为单位检测 CLLocationCoordinate2D 附近的触摸
Detect touch nearby to CLLocationCoordinate2D in pixels
我在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;
}
}
}
但我当然不喜欢这个解决方案,因为区域会随着缩放比例的变化而变化。我想要像标记周围手指宽度的东西被禁止长按。这个距离怎么计算?
在 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
}
}
}
我在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;
}
}
}
但我当然不喜欢这个解决方案,因为区域会随着缩放比例的变化而变化。我想要像标记周围手指宽度的东西被禁止长按。这个距离怎么计算?
在 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
}
}
}