IGoogleMap.java
/* 
 * Copyright 2009 IT Mill Ltd.
 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 
 * http://www.apache.org/licenses/LICENSE-2.0
 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.itmill.toolkit.demo.reservation.gwt.client.ui;

import java.util.Iterator;

import com.google.gwt.maps.client.InfoWindowContent;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.control.SmallMapControl;
import com.google.gwt.maps.client.event.MarkerClickHandler;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.user.client.ui.Composite;
import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;
import com.itmill.toolkit.terminal.gwt.client.Paintable;
import com.itmill.toolkit.terminal.gwt.client.UIDL;

public class IGoogleMap extends Composite implements Paintable {

    public static final String CLASSNAME = "i-googlemap";

    private final MapWidget widget = new MapWidget();

    public IGoogleMap() {
        initWidget(widget);
        setWidth("200px");
        setHeight("200px");
        setStyleName(CLASSNAME);
        widget.addControl(new SmallMapControl());

    }

    public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
        widget.clearOverlays();
        LatLng pos = null;
        for (final Iterator it = uidl.getChildIterator(); it.hasNext();) {
            final UIDL u = (UIDLit.next();
            if (u.getTag().equals("markers")) {

                for (final Iterator m = u.getChildIterator(); m.hasNext();) {
                    final UIDL umarker = (UIDLm.next();
                    final String html = "<span>"
                            + umarker.getStringAttribute("html""</span>";
                    final double x = umarker.getDoubleAttribute("x");
                    final double y = umarker.getDoubleAttribute("y");
                    pos = LatLng.newInstance(x, y);
                    final Marker marker = new Marker(pos);
                    widget.addOverlay(marker);
                    if (html != null) {
                        addMarkerPopup(marker, html);
                    }
                }
            }
        }
        if (uidl.hasAttribute("width")) {
            widget.setWidth(uidl.getStringAttribute("width"));
        }
        if (uidl.hasAttribute("height")) {
            widget.setHeight(uidl.getStringAttribute("height"));
        }
        if (uidl.hasAttribute("zoom")) {
            widget.setZoomLevel(uidl.getIntAttribute("zoom"));
        }
        if (uidl.hasAttribute("centerX"&& uidl.hasAttribute("centerY")) {
            final LatLng center = LatLng.newInstance(uidl
                    .getDoubleAttribute("centerX"), uidl
                    .getDoubleAttribute("centerY"));
            widget.setCenter(center);
        else if (pos != null) {
            // use last marker position
            widget.setCenter(pos);
        }

    }

    private void addMarkerPopup(Marker marker, final String html) {
        marker.addMarkerClickHandler(new MarkerClickHandler() {

            public void onClick(MarkerClickEvent event) {
                widget.getInfoWindow().open(event.getSender().getPoint(),
                        new InfoWindowContent(html));

            }

        });

    }

}