2013-12-16 33 views
0

我想在用户将鼠标悬停在GWT-openlayers地图上的矢量要素时显示自定义工具提示(弹出)。我知道SelectFeature.setHover()将允许我这样做,但也会选择我不想要的功能。如何将MouseOver处理函数添加到GWT-Openlayers中的VectorFeature中

它就像是当用户悬停时,必须显示工具提示,并且当他点击该特征时,选择缪斯会发生。

这怎么能实现?

问候 Jatin

回答

0

看看这个代码。 这也将被添加到我们的展示中,但我们目前在服务器上有一些小问题。上传时会在这里留言。

请注意,添加SelectFeature控件的顺序很重要。

public void buildPanel() { 
    // create some MapOptions 
    MapOptions defaultMapOptions = new MapOptions(); 
    defaultMapOptions.setNumZoomLevels(16); 

    // Create a MapWidget 
    final MapWidget mapWidget = new MapWidget("500px", "500px", 
      defaultMapOptions); 

    // Create an EmptyLayer as base layer 
    EmptyLayer.Options emptyLayerOptions = new EmptyLayer.Options(); 
    emptyLayerOptions.setAttribution("EmptyLayer (c) GWT-Openlayers"); 
    emptyLayerOptions.setIsBaseLayer(true); // make it a baselayer. 
    EmptyLayer emptyLayer = new EmptyLayer("Empty layer", emptyLayerOptions); 
    mapWidget.getMap().addLayer(emptyLayer); 

    // Add a clickable vectors to the map 

    // create a layer to add the vectors to 
    final Vector vectorLayer = new Vector("Vectorlayer"); 
    mapWidget.getMap().addLayer(vectorLayer); 

    // SelectFeature control to capture clicks on the vectors 
    final SelectFeature selectFeature = new SelectFeature(vectorLayer); 
    selectFeature.setAutoActivate(true); 

    // SelectFeature control to capture hover on the vectors 
    SelectFeatureOptions selectFeatureHoverOptions = new SelectFeatureOptions(); 
    // use the tempory style to be defined in the StyleMap  
    selectFeatureHoverOptions.setRenderIntent(RenderIntent.TEMPORARY); 
    selectFeatureHoverOptions.setHighlightOnly(true); 
    selectFeatureHoverOptions.setHover(); 
    SelectFeature selectHoverFeature = new SelectFeature(vectorLayer, 
      selectFeatureHoverOptions); 
    selectHoverFeature.setClickOut(false); 
    selectHoverFeature.setAutoActivate(true); 

    mapWidget.getMap().addControl(selectHoverFeature); 
    mapWidget.getMap().addControl(selectFeature); 

    // Define a style for the vectors 
    Style style = new Style(); 
    style.setFillColor("red"); 
    style.setStrokeColor("green"); 
    style.setStrokeWidth(2); 
    style.setFillOpacity(0.9); 
    style.setPointRadius(30); 

    Style selectedStyle = new Style(); 
    selectedStyle.setFillColor("yellow"); 
    selectedStyle.setStrokeColor("yellow"); 
    selectedStyle.setStrokeWidth(2); 
    selectedStyle.setFillOpacity(0.9); 
    selectedStyle.setPointRadius(30); 

    Style hoverStyle = new Style(); 
    hoverStyle.setFillColor("blue"); 
    hoverStyle.setStrokeColor("pink"); 
    hoverStyle.setStrokeWidth(2); 
    hoverStyle.setFillOpacity(0.9); 
    hoverStyle.setPointRadius(30); 

    StyleMap styleMap = new StyleMap(style, selectedStyle, hoverStyle); 
    vectorLayer.setStyleMap(styleMap); 

    // Add a point 
    Point point = new Point(146.7, -41.8); 
    final VectorFeature pointFeature = new VectorFeature(point); 
    vectorLayer.addFeature(pointFeature); 

    // capture clicks on the vectorlayer 
    vectorLayer 
      .addVectorFeatureSelectedListener(new VectorFeatureSelectedListener() { 
       public void onFeatureSelected(
         FeatureSelectedEvent eventObject) { 
        Window.alert("The vector is now selected.\nIt will get de-selected when closing this popup."); 
        selectFeature.unSelect(eventObject.getVectorFeature()); 
       } 
      }); 

    // Attach a popup to the point, we use null as size cause we set 
    // autoSize to true 
    // Note that we use FramedCloud... This extends a normal popup and 
    // creates is styled as a baloon 
    // We want to display this popup on hover, and hide it when hovering 
    // ends 

    final Popup popup = new FramedCloud("id1", 
      pointFeature.getCenterLonLat(), null, 
      "<h1>Some popup text</H1><BR/>And more text", null, false); 
    popup.setPanMapIfOutOfView(true); // this set the popup in a strategic 
             // way, and pans the map if needed. 
    popup.setAutoSize(true); 
    pointFeature.setPopup(popup); 

    // capture hover by adding a listener to the control, and display the 
    // popup 
    selectHoverFeature 
      .addFeatureHighlightedListener(new FeatureHighlightedListener() { 

       public void onFeatureHighlighted(VectorFeature vectorFeature) { 
        mapWidget.getMap().addPopup(vectorFeature.getPopup()); 
       } 
      }); 

    // capture unhover, and remove popup 
    selectHoverFeature 
      .addFeatureUnhighlightedListener(new FeatureUnhighlightedListener() { 

       public void onFeatureUnhighlighted(
         VectorFeature vectorFeature) { 
        mapWidget.getMap() 
          .removePopup(vectorFeature.getPopup()); 
       } 
      }); 

    // Center and zoom to a location 
    mapWidget.getMap().setCenter(new LonLat(146.7, -41.8), 6); 

    contentPanel 
      .add(new HTML(
        "<p>This example shows how to add a Vector (point) to map, and do some action when hovering, and another when clicking.</p>" 
          + "<p>" 
          + "<LI>Hover over the point. This will cause a popup to show, and change the style of the point to the temporary style.</LI>" 
          + "<LI>Then when you click the Vector gets selected, gets another style, and a Window.alert is displayed.</LI>" 
          + "<LI>When closing the Window.alert, the Vector gets de-selected.</p>")); 

    contentPanel.add(mapWidget); 

    initWidget(contentPanel); 

    mapWidget.getElement().getFirstChildElement().getStyle().setZIndex(0); 
} 
+0

在线展示示例:http://demo.gwt-openlayers.org/gwt_ol_showcase/GwtOpenLayersShowcase.html?example=Hover%20and%20select%20vector%20example – Knarf

+0

很棒!谢谢 – user2163450

0

你的确使用SelectFeature实现这一目标。 秘诀在于您必须通过SelectFeatureOptions且只有启用了高亮。

喜欢的东西

SelectFeatureOptions selectFeatureOptions = new SelectFeatureOptions(); 
    selectFeatureOptions.setHighlightOnly(true); 

    SelectFeature selectFeature = new SelectFeature(vectorLayer,selectFeatureOptions); 
+0

Hello Knarn, 感谢您的回复。 你的建议有效,但是还有一个不同的问题。 当我设置建议的设置,现在我无法选择鼠标点击功能。 只要做到这一点,并移动我的鼠标,该功能将被取消选中。 正如我的问题所说,我需要2件事 1.在悬停时,我必须显示工具提示而不选择功能,并且当我点击该功能时,它必须被选中。 任何进一步的帮助表示赞赏。 Regards, Jatin – user2163450

+0

我将详细了解下列日子之一。 (请注意,我是GWT-OpenLayers的贡献者之一)。 我会告诉你 – Knarf

相关问题