Google Maps API v3 - Buttons and TextBoxes inside InfoWindow?

前端 未结 2 599
闹比i
闹比i 2021-01-07 04:10

I\'m using the new maps v3 API from gwt-google-apis.

Is it possible to capture events from GWT widgets that are inside InfoWindow? Am I missing something?

Tr

相关标签:
2条回答
  • 2021-01-07 04:36

    The problem is result of a broken hierarchy between the widgets, the normal way to do it is by attach / detach widget. You do it by setting of the widget's element. This is also matter of Google Maps API.

    This can be resolved by using fake panel which will be part of the InfoWindow, so when you make setContent(Widget widget) the fake panel will be updated and the element of the widget will be set to the content (as previous).

    Please take a look at this class:

    public class MyInfoWindow extends InfoWindow {
    
      static class FakePanel extends ComplexPanel {
        public FakePanel(Widget w) {
          w.removeFromParent();
          getChildren().add(w);
          adopt(w);
        }
    
        @Override
        public boolean isAttached() {
          return true;
        }
    
        public void detachWidget() {
          this.remove(0);
        }
      }
    
      private IsWidget widgetContent = null;
    
      FakePanel widgetAttacher;
    
      public MyInfoWindow() {
        super(InfoWindowImpl.impl.construct());
      }
    
      private void detachWidget() {
        if (this.widgetAttacher != null) {
          this.widgetAttacher.detachWidget();
          this.widgetAttacher = null;
        }
      }
    
      public void close() {
        super.close();
        detachWidget();
      }
    
      public void setContent(String content) {
        this.widgetContent = null;
        this.detachWidget();
        super.setContent(content);
      }
    
      /** */
      public void setContent(Widget value) {
        this.widgetContent = value;
        setContent(value.getElement());
    
        if (this.widgetAttacher == null) {
          addListener(getJso(), "closeclick", new Runnable() {
            @Override
            public void run() {
              detachWidget();
            }
          });
          this.widgetAttacher = new FakePanel(value);
        } else if (this.widgetAttacher.getWidget(0) != value) {
          this.widgetAttacher.detachWidget();
          this.widgetAttacher = new FakePanel(value);
        }
      }
    
      private void setContent(Element element) {
        InfoWindowImpl.impl.setContent(getJso(), element);
      }
    
      public IsWidget getContentWidget() {
        return widgetContent;
      }
    
      public final native void addListener(JavaScriptObject jso, String whichEvent, Runnable handler)
      /*-{
        var that = jso;
        $wnd.google.maps.event.addListener(jso, whichEvent, function() {
          handler.@java.lang.Runnable::run()();
        });
      }-*/;
    
    }
    
    0 讨论(0)
  • 2021-01-07 04:39

    I had to build a wrapper over InfoWindow to make it work.

    public class NXInfoWindow {
    
        static class FakePanel extends ComplexPanel {
            public FakePanel(Widget w) {
                w.removeFromParent();
                getChildren().add(w);
                adopt(w);
            }
    
            @Override
            public boolean isAttached() {
                return true;
            }
    
            public void detachWidget() {
                this.remove(0);
            }
        }
    
        private InfoWindow info;
    
        private IsWidget widgetContent = null;
    
        private Long id;
    
        FakePanel widgetAttacher;
    
        public static NXInfoWindow create(Long id){
            NXInfoWindow myInfo = new NXInfoWindow();
            myInfo.info = InfoWindow.create(); 
            myInfo.id = id;
            return myInfo;
        };
    
        private void detachWidget() {
            if (this.widgetAttacher != null) {
                this.widgetAttacher.detachWidget();
                this.widgetAttacher = null;
            }
        }
    
        public void close() {
            info.close();
            detachWidget();
        }
    
        public void setPosition(LatLng posicao) {
            info.setPosition(posicao);
        }
    
        public void open(GoogleMap map) {
            info.open(map);
        }
    
        public void setContent(Widget value) {
            this.widgetContent = value;
            info.setContent(value.getElement());
    
            if (this.widgetAttacher == null) {
                addListener(info, "closeclick", new Runnable() {
                    @Override
                    public void run() {
                        detachWidget();
                    }
                });
                this.widgetAttacher = new FakePanel(value);
            } else if (this.widgetAttacher.getWidget(0) != value) {
                this.widgetAttacher.detachWidget();
                this.widgetAttacher = new FakePanel(value);
            }
        }
    
        private void setContent(Element element) {
            this.setContent(element);
        }
    
        public IsWidget getContentWidget() {
            return widgetContent;
        }
    
        public final native void addListener(JavaScriptObject jso, String whichEvent, Runnable handler)
        /*-{
            var that = jso;
            $wnd.google.maps.event.addListener(jso, whichEvent, function() {
              handler.@java.lang.Runnable::run()();
            });
          }-*/;
    }
    
    0 讨论(0)
提交回复
热议问题