001    package com.google.gwt.maps.client;
002    
003    import java.util.ArrayList;
004    import java.util.Iterator;
005    
006    import com.google.gwt.ajaxloader.client.AjaxLoader;
007    import com.google.gwt.ajaxloader.client.AjaxLoader.AjaxLoaderOptions;
008    
009    /**
010     * Load Maps javascript v3 api
011     * 
012     * TODO maybe move to the new loading system they have in google apis
013     */
014    public class LoadApi {
015    
016            /**
017             * Note: If you use 3, it will take the newest stable available. Don't want that. We didn't test with that yet!
018             */
019            public final static String API_VERSION = "3.9";
020            
021            /**
022             * Libraries not loaded by default <br>
023             * <br>
024             * See <a href=
025             * "https://developers.google.com/maps/documentation/javascript/basics.html#Libraries"
026             * >Libraries API Doc</a>
027             */
028            public static enum LoadLibrary {
029    
030                    /**
031                     * allows your Maps API application to include context-sensitive text
032                     * ads, allowing you to share in ad revenue for ads shown to your users.
033                     * Consult the AdSense Library documentation for more information.
034                     */
035                    ADSENSE,
036    
037                    /**
038                     * provides a graphical interface for users to draw polygons,
039                     * rectangles, polylines, circles, and markers on the map. Consult the
040                     * Drawing Library documentation for more information.
041                     */
042                    DRAWING,
043    
044                    /**
045                     * geometry includes utility functions for calculating scalar geometric
046                     * values (such as distance and area) on the the surface of the earth.
047                     * Consult the Geometry Library documentation for more information.
048                     */
049                    GEOMETRY,
050    
051                    /**
052                     * panoramio contains functionality for adding Panoramio photo layers to
053                     * your Maps API application. Consult the Panoramio Layers documentation
054                     * for more information.
055                     */
056                    PANORAMIO,
057    
058                    /**
059                     * weather contains functionality for adding meterological layers to
060                     * your Maps API application. Consult the Weather Layers documentation
061                     * for more information.
062                     */
063                    WEATHER,
064    
065                    /**
066                     * places enables your application to search for businesses, geographic
067                     * locations, and points of interest near a given location, or as a user
068                     * types. Consult the Places Library documentation for more information.
069                     */
070                    PLACES;
071    
072                    public String value() {
073                            return name().toLowerCase();
074                    }
075    
076                    public static LoadLibrary fromValue(String value) {
077                            return valueOf(value.toUpperCase());
078                    }
079    
080                    @Override
081                    public String toString() {
082                            return name().toLowerCase();
083                    }
084            }
085    
086            /**
087             * Load Maps javascript v3 api with default libraries. these are not loaded
088             * {@link LoadLibrary}
089             * 
090             * @param onLoad
091             *            - callback on success
092             * @param sensor
093             *            - derive location [true|false]
094             */
095            public static void go(Runnable onLoad, boolean sensor) {
096                    load(onLoad, sensor, null, null);
097            }
098    
099            /**
100             * loads maps api
101             * 
102             * @param onLoad
103             *            callback on success
104             * @param loadLibraries
105             *            load additional libraries like geometry
106             * @param sensor
107             *            derive location [true|false]
108             */
109            public static void go(Runnable onLoad,
110                            ArrayList<LoadLibrary> loadLibraries, boolean sensor) {
111                    load(onLoad, sensor, loadLibraries, null);
112            }
113    
114            /**
115             * Load Maps javascript v3 api
116             * 
117             * @param onLoad
118             *            callback on success
119             * @param sensor
120             *            derive location [true|false]
121             * @param otherParams
122             *            add additional params. like "key=YOUR_API_KEY"
123             */
124            public static void go(Runnable onLoad, boolean sensor, String otherParams) {
125                    load(onLoad, sensor, null, otherParams);
126            }
127    
128            /**
129             * loads maps api
130             * 
131             * @param onLoad
132             *            callback on success
133             * @param loadLibraries
134             *            load additional libraries like geometry
135             * @param sensor
136             *            sensor derive location [true|false]
137             * @param otherParams
138             *            add additional params. like "key=YOUR_API_KEY"
139             */
140            public static void go(Runnable onLoad,
141                            ArrayList<LoadLibrary> loadLibraries, boolean sensor,
142                            String otherParams) {
143                    load(onLoad, sensor, loadLibraries, otherParams);
144            }
145    
146            /**
147             * load the maps library
148             * 
149             * @param onLoad
150             * @param sensor
151             * @param loadLibraries
152             * @param otherParams
153             */
154            private static void load(Runnable onLoad, boolean sensor,
155                            ArrayList<LoadLibrary> loadLibraries, String otherParams) {
156    
157                    String op = "sensor=" + sensor;
158                    if (otherParams != null) {
159                            op += "&" + otherParams;
160                    }
161    
162                    if (loadLibraries != null) {
163                            op += "&" + getLibraries(loadLibraries);
164                    }
165    
166                    AjaxLoaderOptions settings = AjaxLoaderOptions.newInstance();
167                    settings.setOtherParms(op);
168                    AjaxLoader.loadApi("maps", API_VERSION, onLoad, settings);
169            }
170    
171            /**
172             * get the url libraries parameter
173             * 
174             * @param loadLibraries
175             */
176            private static String getLibraries(ArrayList<LoadLibrary> loadLibraries) {
177                    if (loadLibraries == null) {
178                            return "";
179                    }
180                    String s = "libraries=";
181                    Iterator<LoadLibrary> itr = loadLibraries.iterator();
182                    int i = 0;
183                    while (itr.hasNext()) {
184                            LoadLibrary ll = itr.next();
185                            if (ll != null) {
186                                    if (i > 0) {
187                                            s += ",";
188                                    }
189                                    s += ll.value();
190                                    i++;
191                            }
192                    }
193                    return s;
194            }
195    
196            /**
197             * private constructor
198             */
199            private LoadApi() {
200            }
201    }