简体   繁体   中英

GWT Google Maps V3 overlay widgets

I'm using the GWT google maps V3 API and I need to display custom shapes over the map. For simple elements I used Polygon, Circle and InfoWidnow classes, but I want to display some other widgets like button or custom panels. Is there any way to do that using OverlayView class? I tried a simple solution: an AbsolutePanel that contains the MapWidget and my custom widgets, placed on top, but I would like to use the gwt google maps classes as much as I can. I've read the documentation and also searched for an answer but I could't figure it out so a code example would be great. Thanx!

Just make use of standard GWT API alongside with your maps V3 API. They will interconnect well enough.

I found what i needed here (the javascript v3 api): https://developers.google.com/maps/documentation/javascript/overlays#CustomOverlays

the method names and classes are very similar so it isn't so difficult to figure out how the GWT classes should be used (like OverlayView).

Here is an example with custom widgets (containing SVG elements and animation) rendered on the map:

private class TargettingOverlay extends OverlayView {

    protected Element div ;
    protected PanelWrapper panelWrapper;
    private TargettingEffect targetEffect;

    TargettingOverlay(){
        targetEffect = new TargettingEffect();
        targetEffect.setLinedDimension(10500);
        targetEffect.setLinesOffset(-5000);
    }

    void positionTarget(LatLng loc, boolean withoutLines){
        if (targetEffect == null )
            return;

        if (loc == null) {
            targetEffect.setElementsVisible(false);
            return;
        }
        targetEffect.setElementsVisible(true);


        Point p = null;
        Point sw = null;
        Point ne = null;
        LatLng locSW = (LatLng)this.getMap().getBounds().getSouthWest();
        LatLng locNE = (LatLng)this.getMap().getBounds().getNorthEast();

        //
        p = (Point)getProjection().fromLatLngToDivPixel(loc);
        sw = (Point)getProjection().fromLatLngToDivPixel(locSW);
        ne = (Point)getProjection().fromLatLngToDivPixel(locNE);



        targetEffect.setWithoutLinles(withoutLines);
        targetEffect.target((int)ne.getY(), (int)p.getY(), (int)sw.getX(), (int)p.getX());
    }

    @Override
    public void draw() {
        Point ne = (Point)getProjection().fromLatLngToDivPixel((LatLng)
                this.getMap().getBounds().getNorthEast());
        Point sw = (Point)getProjection().fromLatLngToDivPixel((LatLng)
                this.getMap().getBounds().getSouthWest());


        div.getStyle().setTop(ne.getY(), Unit.PX);
        div.getStyle().setLeft(sw.getX(), Unit.PX); 
    }

    @Override
    public void onAdd() {
        div = DOM.createDiv();

        getPanes().getOverlayLayer().appendChild(div);

        panelWrapper = new PanelWrapper(div);
        panelWrapper.attach();          

        targetEffect.setContainer(panelWrapper);
    }

    @Override
    public void onRemove() {
        div.removeFromParent();
        panelWrapper.removeFromParent();

        div = null;
        panelWrapper = null;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM