You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VGoogleMap.java 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.demo.reservation.gwt.client.ui;
  5. import java.util.Iterator;
  6. import com.google.gwt.maps.client.InfoWindowContent;
  7. import com.google.gwt.maps.client.MapWidget;
  8. import com.google.gwt.maps.client.control.SmallMapControl;
  9. import com.google.gwt.maps.client.event.MarkerClickHandler;
  10. import com.google.gwt.maps.client.geom.LatLng;
  11. import com.google.gwt.maps.client.overlay.Marker;
  12. import com.google.gwt.user.client.ui.Composite;
  13. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  14. import com.vaadin.terminal.gwt.client.Paintable;
  15. import com.vaadin.terminal.gwt.client.UIDL;
  16. public class VGoogleMap extends Composite implements Paintable {
  17. public static final String CLASSNAME = "i-googlemap";
  18. private final MapWidget widget = new MapWidget();
  19. public VGoogleMap() {
  20. initWidget(widget);
  21. setWidth("200px");
  22. setHeight("200px");
  23. setStyleName(CLASSNAME);
  24. widget.addControl(new SmallMapControl());
  25. }
  26. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  27. widget.clearOverlays();
  28. LatLng pos = null;
  29. for (final Iterator it = uidl.getChildIterator(); it.hasNext();) {
  30. final UIDL u = (UIDL) it.next();
  31. if (u.getTag().equals("markers")) {
  32. for (final Iterator m = u.getChildIterator(); m.hasNext();) {
  33. final UIDL umarker = (UIDL) m.next();
  34. final String html = "<span>"
  35. + umarker.getStringAttribute("html") + "</span>";
  36. final double x = umarker.getDoubleAttribute("x");
  37. final double y = umarker.getDoubleAttribute("y");
  38. pos = LatLng.newInstance(x, y);
  39. final Marker marker = new Marker(pos);
  40. widget.addOverlay(marker);
  41. if (html != null) {
  42. addMarkerPopup(marker, html);
  43. }
  44. }
  45. }
  46. }
  47. if (uidl.hasAttribute("width")) {
  48. widget.setWidth(uidl.getStringAttribute("width"));
  49. }
  50. if (uidl.hasAttribute("height")) {
  51. widget.setHeight(uidl.getStringAttribute("height"));
  52. }
  53. if (uidl.hasAttribute("zoom")) {
  54. widget.setZoomLevel(uidl.getIntAttribute("zoom"));
  55. }
  56. if (uidl.hasAttribute("centerX") && uidl.hasAttribute("centerY")) {
  57. final LatLng center = LatLng.newInstance(uidl
  58. .getDoubleAttribute("centerX"), uidl
  59. .getDoubleAttribute("centerY"));
  60. widget.setCenter(center);
  61. } else if (pos != null) {
  62. // use last marker position
  63. widget.setCenter(pos);
  64. }
  65. }
  66. private void addMarkerPopup(Marker marker, final String html) {
  67. marker.addMarkerClickHandler(new MarkerClickHandler() {
  68. public void onClick(MarkerClickEvent event) {
  69. widget.getInfoWindow().open(event.getSender().getPoint(),
  70. new InfoWindowContent(html));
  71. }
  72. });
  73. }
  74. }