Friday, February 22, 2013

GWT Maps api v3 Custom overlays

to Implement GWT widgets using Google Maps  v3 :


import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.dom.client.Style;
import com.google.gwt.maps.client.HasMap;
import com.google.gwt.maps.client.base.LatLng;
import com.google.gwt.maps.client.base.Point;
import com.google.gwt.maps.client.overlay.OverlayView;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.SimplePanel;


public class GWTMapWidget 
{
    private Element     div;
    private LatLng      latLng;
    private SimplePanel textPanel;
    private String      text;


    private OverlayView overlay = new OverlayView ()
    {
        @Override
        public void draw ()
        {
            Point sw = (Point) overlay.getProjection ().fromLatLngToDivPixel (overlay.getMap ().getBounds ().getSouthWest ());
            Point ne = (Point) overlay.getProjection ().fromLatLngToDivPixel (overlay.getMap ().getBounds ().getNorthEast ());

            Point p = (Point) overlay.getProjection ().fromLatLngToDivPixel (latLng);

        //    System.out.println (p.getX () + " " + p.getY ());
        //      div.getStyle ().setTop (ne.getY (), Style.Unit.PX);
        //     div.getStyle ().setLeft (sw.getX (), Style.Unit.PX);

            div.getStyle ().setTop (p.getY (), Style.Unit.PX);
            div.getStyle ().setLeft (p.getX (), Style.Unit.PX);


            div.getStyle ().setWidth (ne.getX ()-sw.getX (), Style.Unit.PX);
            div.getStyle ().setHeight (sw.getY ()-sw.getY (), Style.Unit.PX);

            div.getStyle ().setDisplay (Style.Display.BLOCK);
            div.getStyle ().setZIndex (99);

        }

        @Override
        public void onAdd ()
        {
            div = DOM.createElement ("div");
            div.getStyle ().setBorderStyle (Style.BorderStyle.NONE);

            div.getStyle ().setBorderWidth (0, Style.Unit.PX);
            div.getStyle ().setPosition (Style.Position.ABSOLUTE);

            div.appendChild (textPanel.getElement ());
            overlay.getPanes ().getFloatPane ().appendChild (div);
        }

        @Override
        public void onRemove ()
        {
            div.getParentNode ().removeChild (div);
            // div.removeFromParent ();
            textPanel.removeFromParent ();

            div = null;
            textPanel = null;
        }
    };

    public GWTMapWidget (LatLng latLng, TextPanel textPanel , HasMap map)
    {

        this.latLng = latLng;

        // System.out.println (latLng.getLatitude () + " - " + latLng.getLongitude ());
        this.text = text;

        HTML textWidget = new HTML (text);

       this. textPanel = textPanel ;      


        overlay.setMap (map);
    }

}

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment