事件

地图和地图要素具有下列事件。

地图事件

MapClickEvent

用户点击地图触发的事件。注意,如果用户双击地图,该事件会触发两次。

下面是处理地图点击事件的示例。用户点击时,显示一条消息,包含事件的名称和地图中点击的坐标:

protected GeometryFactory geometryFactory = GeometryUtils.getGeometryFactory();

@Autowired
private Notifications notifications;

@Subscribe("geoMap")
public void onGeoMapMapClick(final MapClickEvent event) {
    Point point = geometryFactory.createPoint(event.getCoordinate());
    notifications.create("ClickEvent", point.getCoordinate().toString())
            .withPosition(Notification.Position.BOTTOM_END)
            .show();
}

MapSingleClickEvent

地图单击事件会延迟 250ms 发送,以确保用户不是双击。

MapDoubleClickEvent

用户双击地图时发送的事件。

下面是处理地图双击事件的示例。

@ViewComponent
private GeoMap geoMap;

@Subscribe("geoMap")
public void onGeoMapMapDoubleClick(final MapDoubleClickEvent event) {
    GeoMapView view = new GeoMapView();
    view.setCenter(event.getCoordinate());
    view.setZoom(6);
    geoMap.setMapView(view);
}

MapMoveEndEvent

地图移动时发送的事件,包括缩放、平移或者拖动等。

下面是处理地图移动事件的示例。

@Autowired
private Notifications notifications;

@Subscribe("geoMap")
public void onGeoMapMapMoveEnd(final MapMoveEndEvent event) {
    notifications.create("MapMoveEndEvent",
                    String.format("""
                                Values:
                                Center: %s
                                Zoom: %s
                                Rotation: %s
                                """,
                            event.getCenter(),
                            event.getZoom(),
                            event.getRotation()))
            .withPosition(Notification.Position.BOTTOM_END)
            .show();
}

MapZoomChangedEvent

地图缩放发生变化时发送的事件。

下面是处理地图缩放事件的示例。

@Autowired
private Notifications notifications;

@Subscribe("geoMap")
public void onGeoMapMapZoomChanged(final MapZoomChangedEvent event) {
    notifications.create("MapZoomChangedEvent", "Zoom: " + event.getZoom())
            .withPosition(Notification.Position.BOTTOM_END)
            .show();
}

源事件

SourceFeatureClickEvent

用户点击 地图要素 时触发的事件。注意,如果用户双击,该事件会触发两次。

@ViewComponent("geoMap.vector.vectorSource")
private VectorSource vectorSource;

@Subscribe
public void onInit(final InitEvent event) {
    vectorSource.addSourceFeatureClickListener(clickEvent -> {
        Feature feature = clickEvent.getFeature();
        notifications.create("SourceFeatureClickEvent",
                        feature.getGeometry().get().getClass().getSimpleName())
                .withPosition(Notification.Position.BOTTOM_END)
                .show();
    });
}

SourceFeatureSingleClickEvent

地图要素单击事件会延迟 250ms 发送,以确保用户不是双击。

SourceFeatureDoubleClickEvent

用户双击地图要素时发送的事件。

SourceFeatureSelectEvent

该事件在选择或取消选择地图要素时触发。事件中仅包括刚选择或取消选择的要素。例如,如果已经选择了两个要素之一,此时选择剩下一个要素时,触发的该事件内仅包含第二个要素。

@Autowired
private Notifications notifications;

@Subscribe("map.featureLayer.featureSource")
public void onMapFeatureLayerFeatureSourceSourceFeatureSelect(final HasFeatureSelect.SourceFeatureSelectEvent event) {
    notifications.create("SourceFeatureSelectEvent",
                    String.format("Selected: %s\nDeselected: %s",
                            event.getSelected().size(), event.getDeselected()))
            .show();
}

SourceFeatureDragStartEvent

该事件在用户开始拖拽地图要素时触发。

@Autowired
private Notifications notifications;

@Subscribe("map.featureLayer.featureSource")
public void onSourceFeatureDragStart(final HasFeatureDrag.SourceFeatureDragStartEvent event) {
    notifications.create("SourceFeatureDragStartEvent", "Features: "
                    + event.getFeatures().size())
            .show();
}

SourceFeatureDragEndEvent

该事件在用户完成拖拽地图要素时触发。

@Autowired
private Notifications notifications;

@Subscribe("map.featureLayer.featureSource")
public void onSourceFeatureDragEnd(final HasFeatureDrag.SourceFeatureDragEndEvent event) {
    notifications.create("SourceFeatureDragEndEvent", "Features: "
                    + event.getFeatures().size())
            .show();
}

SourceFeatureModifyStartEvent

该事件在开始编辑地图要素时触发。

@Autowired
private Notifications notifications;

@Subscribe("map.featureLayer.featureSource")
public void onSourceFeatureModifyStart(final HasFeatureModify.SourceFeatureModifyStartEvent event) {
    notifications.create("SourceFeatureModifyStartEvent", "Features: "
                    + event.getFeatures().size())
            .show();
}

SourceFeatureModifyEndEvent

该事件在完成编辑地图要素时触发。

@Autowired
private Notifications notifications;

@Subscribe("map.featureLayer.featureSource")
public void onSourceFeatureModifyEnd(final HasFeatureModify.SourceFeatureModifyEndEvent event) {
    notifications.create("SourceFeatureModifyEndEvent", "Features: "
                    + event.getFeatures().size())
            .show();
}

SourceFeatureDeleteEvent

当从源中删除一个要素时触发。

@Autowired
private Notifications notifications;

@Subscribe("map.featureLayer.featureSource")
public void onSourceFeatureDelete(final HasFeatureModify.SourceFeatureDeleteEvent event) {
    notifications.create("SourceFeatureDeleteEvent", "Features: "
                    + event.getFeatures().size())
            .show();
}

GeoObjectClickEvent

用户点击地理对象时发送的事件。注意,如果用户双击,该事件会触发两次。

@Autowired
private Notifications notifications;

@ViewComponent("map.vectorLayer.dataVectorSource")
private DataVectorSource<Location> dataVectorSource;

@Subscribe
public void onInit(final InitEvent event) {
    dataVectorSource.addGeoObjectClickListener(clickEvent ->
            notifications.create("GeoObject click", clickEvent.getItem().getCity())
            .withPosition(Notification.Position.BOTTOM_END)
            .show());
}

GeoObjectSingleClickEvent

地理对象单击事件会延迟 250ms 发送,以确保用户不是双击。

GeoObjectDoubleClickEvent

用户双击地理对象时发送的事件。

GeoObjectSelectEvent

该事件在选择或取消选择地理对象时触发。事件中仅包括刚选择或取消选择的对象。例如,如果已经选择了两个地理对象之一,此时选择剩下一个对象时,触发的该事件内仅包含第二个对象。

@Autowired
private Notifications notifications;

@Subscribe("map.layer.source")
public void onMapLayerSourceGeoObjectSelect(final HasGeoObjectSelect.GeoObjectSelectEvent<Region> event) {
    notifications.create("GeoObjectSelectEvent",
                    String.format("Selected: %s\nDeselected: %s",
                            event.getSelected().size(), event.getDeselected()))
            .show();
}

GeoObjectDragStartEvent

该事件在用户开始拖拽地理对象时触发。

@Autowired
private Notifications notifications;

@Subscribe("map.layer.source")
public void onSourceGeoObjectDragStart(final HasGeoObjectDrag.GeoObjectDragStartEvent<Region> event) {
    notifications.create("GeoObjectDragStartEvent", "Items: "
                    + event.getItems().size())
            .show();
}

GeoObjectDragEndEvent

该事件在用户完成拖拽地理对象时触发。

@Autowired
private Notifications notifications;

@Subscribe("map.layer.source")
public void onSourceGeoObjectDragEnd(final HasGeoObjectDrag.GeoObjectDragEndEvent<Region> event) {
    notifications.create("GeoObjectDragEndEvent", "Items: "
                    + event.getItems().size())
            .show();
}

GeoObjectModifyStartEvent

该事件在开始编辑地理对象时触发。

@Autowired
private Notifications notifications;

@Subscribe("map.layer.source")
public void onSourceGeoObjectModifyStart(final HasGeoObjectModify.GeoObjectModifyStartEvent<Region> event) {
    notifications.create("GeoObjectModifyStartEvent", "Items: "
                    + event.getItems().size())
            .show();
}

GeoObjectModifyEndEvent

该事件在完成编辑地理对象时触发。

@Autowired
private Notifications notifications;

@Subscribe("map.layer.source")
public void onSourceGeoObjectModifyEnd(final HasGeoObjectModify.GeoObjectModifyEndEvent<Region> event) {
    notifications.create("GeoObjectModifyEndEvent", "Items: "
                    + event.getItems().size())
            .show();
}

要素事件

FeatureClickEvent

用户点击地图要素时触发的事件。注意,如果用户双击地图,该事件会触发两次。

FeatureSingleClickEvent

地图要素单击事件会延迟 250ms 发送,以确保用户不是双击。

FeatureDoubleClickEvent

用户双击地图要素时发送的事件。