001    package com.google.gwt.maps.client.layers;
002    
003    import com.google.gwt.core.client.JavaScriptObject;
004    import com.google.gwt.event.shared.HandlerRegistration;
005    import com.google.gwt.maps.client.MapImpl;
006    import com.google.gwt.maps.client.MapWidget;
007    import com.google.gwt.maps.client.base.LatLngBounds;
008    import com.google.gwt.maps.client.events.MapEventType;
009    import com.google.gwt.maps.client.events.MapHandlerRegistration;
010    import com.google.gwt.maps.client.events.kmlmouse.KmlMouseEventFormatter;
011    import com.google.gwt.maps.client.events.kmlmouse.KmlMouseMapHandler;
012    import com.google.gwt.maps.client.events.kmlviewport.DefaultViewportChangeEventFormatter;
013    import com.google.gwt.maps.client.events.kmlviewport.DefaultViewportChangeMapHandler;
014    
015    /**
016     * A KmlLayer adds geographic markup to the map from a KML, KMZ or GeoRSS file that is hosted on a publicly accessible web server. A KmlFeatureData object is provided for each feature when clicked. This class extends MVCObject.
017     * <br><br>
018     * See <a href="https://developers.google.com/maps/documentation/javascript/reference#KmlLayer">KmlLayer API Doc</a>
019     */
020    public class KmlLayer extends JavaScriptObject {
021      
022      /**
023       * use newInstance();
024       */
025      protected KmlLayer() {}
026      
027      /**
028       * Creates a KmlLayer which renders the contents of the specified KML/KMZ file (<a href="https://developers.google.com/kml/documentation/kmlreference">KML API Doc</a>) or GeoRSS file (<a href="http://www.georss.org">GeoRSS API Doc</a>). 
029       * @param url 
030       * @param options {@link KmlLayerOptions}
031       * @return {@link KmlLayerOptions}
032       */
033      public final static KmlLayer newInstance(String url, KmlLayerOptions options) {
034        return createJso(url, options).cast();
035      }
036    
037      /**
038       * Creates a KmlLayer which renders the contents of the specified KML/KMZ file (<a href="https://developers.google.com/kml/documentation/kmlreference">KML API Doc</a>) or GeoRSS file (<a href="http://www.georss.org">GeoRSS API Doc</a>).
039       * @param url
040     */ 
041      public final static KmlLayer newInstance(String url) {
042        return createJso(url).cast();
043      }
044      
045      private static native JavaScriptObject createJso(String url) /*-{
046        return new $wnd.google.maps.KmlLayer(url);
047      }-*/;
048      
049      private static native JavaScriptObject createJso(String url, KmlLayerOptions options) /*-{
050        return new $wnd.google.maps.KmlLayer(url, options);
051      }-*/;
052    
053      /**
054       * Get the default viewport for the layer being displayed.
055       */
056      public final native LatLngBounds getDefaultViewport() /*-{
057        return this.getDefaultViewport();
058      }-*/;
059      
060      /**
061       * Renders the KML Layer on the specified map. If map is set to null, the layer is removed.
062       * @param mapWidget
063       */
064      public final void setMap(MapWidget mapWidget) {
065        if (mapWidget == null) {
066          close();
067        } else {
068          setMapImpl(mapWidget.getJso());
069        }
070      }
071    
072      private final native void setMapImpl(MapImpl map) /*-{
073        this.setMap(map);
074      }-*/;
075      
076      /**
077       * Get the map on which the KML Layer is being rendered.
078       */
079      public final MapWidget getMap() {
080        return MapWidget.newInstance(getMapImpl());
081      }
082    
083      private final native MapImpl getMapImpl() /*-{
084        return this.getMap();
085      }-*/;
086      
087      /**
088       * Get the metadata associated with this layer, as specified in the layer markup.
089       */
090      public final native KmlLayerMetadata getMetadata() /*-{
091        return this.getMetadata();
092      }-*/;
093      
094      /**
095       * Get the URL of the geographic markup which is being displayed.
096       */
097      public final native String getUrl() /*-{
098        return this.getUrl();
099      }-*/;
100      
101      /**
102       * This event is fired when a feature in the layer is clicked.
103       * @param handler
104       */
105      public final HandlerRegistration addClickHandler(KmlMouseMapHandler handler) {
106        return MapHandlerRegistration.addHandler(this, MapEventType.CLICK, handler, new KmlMouseEventFormatter());
107      }
108      
109      /**
110       * This event is fired when the KML layers default viewport has changed.
111       * @param handler
112       */
113      public final HandlerRegistration addDefaultViewportChangeHandler(DefaultViewportChangeMapHandler handler) {
114        return MapHandlerRegistration.addHandler(this, MapEventType.DEFAULTVIEWPORT_CHANGED, handler, new DefaultViewportChangeEventFormatter());
115      }
116      
117      /**
118       * erase kml layer
119       */
120      public final native void close() /*-{
121        this.setMap();
122      }-*/;
123    }