001    package com.google.gwt.maps.client.placeslib;
002    
003    import com.google.gwt.ajaxloader.client.ArrayHelper;
004    import com.google.gwt.core.client.JavaScriptObject;
005    import com.google.gwt.core.client.JsArrayString;
006    import com.google.gwt.dom.client.Element;
007    import com.google.gwt.event.shared.HandlerRegistration;
008    import com.google.gwt.maps.client.base.LatLngBounds;
009    import com.google.gwt.maps.client.events.MapEventType;
010    import com.google.gwt.maps.client.events.MapHandlerRegistration;
011    import com.google.gwt.maps.client.events.place.PlaceChangeEventFormatter;
012    import com.google.gwt.maps.client.events.place.PlaceChangeMapHandler;
013    import com.google.gwt.maps.client.mvc.MVCObject;
014    
015    /**
016     * A service to provide Place predictions based on a user's text input. It attaches to an input element of type text, and listens for text entry in that field. The list of predictions is presented as a drop-down list, and is updated as text is entered. This class extends MVCObject.
017     * <br><br>
018     * See <a href="https://developers.google.com/maps/documentation/javascript/reference#Autocomplete">Autocomplete API Doc</a>
019     */
020    public class Autocomplete extends MVCObject<Autocomplete> {
021    
022      private static final native JavaScriptObject createJso(Element inputField, AutocompleteOptions options) /*-{
023        return new $wnd.google.maps.places.Autocomplete(inputField, options);
024      }-*/;
025      
026      /**
027       * Creates a new instance of Autocomplete that attaches to the specified input text field with the given options.
028       * @param inputField
029       * @param options
030       */
031      public static final Autocomplete newInstance(Element inputField, AutocompleteOptions options) {
032        return createJso(inputField, options).cast();
033      }
034    
035      /**
036       * use newInstance();
037       */
038      protected Autocomplete() {}
039    
040      /**
041       * This event is fired when a PlaceResult is made available for a Place the user has selected. If the user enters the name of a Place that was not suggested by the control and presses the Enter key, a place_changed event will be fired that contains the user input in the name property, with no other properties defined.
042       * @param handler
043       */
044      public final HandlerRegistration addPlaceChangeHandler(PlaceChangeMapHandler handler) {
045        return MapHandlerRegistration.addHandler(this, MapEventType.PLACE_CHANGED, handler, new PlaceChangeEventFormatter());
046      } 
047      
048      /**
049       * Returns the bounds to which predictions are biased.
050       */
051      public final native LatLngBounds getBounds() /*-{
052        return this.getBounds();
053      }-*/;
054      
055      /**
056       * Returns the details of the Place selected by the user, or null if no Place has been selected yet.
057       */
058      public final native PlaceResult getPlace() /*-{
059        return this.getPlace();
060      }-*/;
061      
062      /**
063       * Sets the preferred area within which to return Place results. Results are biased towards, but not restricted to, this area.
064       * @param bounds
065       */
066      public final native void setBounds(LatLngBounds bounds) /*-{
067        this.setBounds(bounds); 
068      }-*/;
069      
070      /**
071       * Sets the types of predictions to be returned. Supported types are 'establishment' for businesses and 'geocode' for addresses. If no type is specified, both types will be returned.
072       * @param types
073       */
074      public final void setTypes(AutocompleteType... types) {
075        if (types == null) {
076          return;
077        }
078        String[] stypes = new String[types.length]; 
079        for (int i=0; i < types.length; i++) {
080          stypes[i] = types[i].value();
081        }
082        JsArrayString a = ArrayHelper.toJsArrayString(stypes);
083        setTypesImpl(a);
084      }
085      
086      private final native void setTypesImpl(JsArrayString types) /*-{
087        this.setTypes(types);
088      }-*/;
089      
090    }