我正在使用谷歌地图 SDK 构建一个 iOS 应用程序。当用户执行 longPressAtCoordinate 时,我可以在 map 上添加一些标记。我的问题是,当我尝试拖动标记时,diiLongPressAtCoordinate 在 didBeginDraggingMarker 之前被触发,因此也添加了一个新标记。
-(void)mapView:(GMSMapView *)mapView didBeginDraggingMarker:(GMSMarker *)marker{
NSLog(@"begin dragging marker");
}
- (void)mapView:(GMSMapView *)mapView didLongPressAtCoordinate (CLLocationCoordinate2D)coordinate{
NSLog(@"did long press at mapview");
//when user didLongPressAtCoordinate I add a new marker on the map.
// I want to prevent the execution of this code before the didBeginDraggingMarker method
}
最佳答案
我通过创建一个名为 isDragging 的 bool 属性并根据标记是否被拖动来更改它的值来解决这个问题。
- (void)mapView:(GMSMapView *)mapView didBeginDraggingMarker:(GMSMarker *)marker
{
self.isDragging = YES;
}
- (void)mapView:(GMSMapView *)mapView didEndDraggingMarker:(GMSMarker *)marker
{
self.isDragging = NO;
}
然后,每当检测到长按时,我都会验证是否正在拖动标记:
- (void)mapView:(GMSMapView *)mapView didLongPressAtCoordinate:(CLLocationCoordinate2D)coordinate
{
if (self.isDragging) {
return;
}
NSLog(@"Long press detected");
}
关于iOS 谷歌地图标记拖动事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27397270/