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.

IGoogleMap.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.demo.reservation.gwt.client.ui;
  5. import java.util.Iterator;
  6. import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;
  7. import com.itmill.toolkit.terminal.gwt.client.Paintable;
  8. import com.itmill.toolkit.terminal.gwt.client.UIDL;
  9. import com.mapitz.gwt.googleMaps.client.GControl;
  10. import com.mapitz.gwt.googleMaps.client.GLatLng;
  11. import com.mapitz.gwt.googleMaps.client.GMap2;
  12. import com.mapitz.gwt.googleMaps.client.GMap2EventManager;
  13. import com.mapitz.gwt.googleMaps.client.GMap2Widget;
  14. import com.mapitz.gwt.googleMaps.client.GMarker;
  15. import com.mapitz.gwt.googleMaps.client.GMarkerEventClickListener;
  16. import com.mapitz.gwt.googleMaps.client.GMarkerEventManager;
  17. public class IGoogleMap extends GMap2Widget implements Paintable {
  18. public static final String CLASSNAME = "i-googlemap";
  19. GMap2EventManager mapEventManager;
  20. GMarkerEventManager markerEventManager;
  21. GMap2 map;
  22. public IGoogleMap() {
  23. setStyleName(CLASSNAME);
  24. mapEventManager = GMap2EventManager.getInstance();
  25. map = getGmap();
  26. map.addControl(GControl.GSmallZoomControl());
  27. }
  28. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  29. map.clearOverlays();
  30. GLatLng pos = null;
  31. for (final Iterator it = uidl.getChildIterator(); it.hasNext();) {
  32. final UIDL u = (UIDL) it.next();
  33. if (u.getTag().equals("markers")) {
  34. for (final Iterator m = u.getChildIterator(); m.hasNext();) {
  35. final UIDL umarker = (UIDL) m.next();
  36. final String html = "<span>"
  37. + umarker.getStringAttribute("html") + "</span>";
  38. final double x = umarker.getDoubleAttribute("x");
  39. final double y = umarker.getDoubleAttribute("y");
  40. pos = new GLatLng(x, y);
  41. final GMarker marker = new GMarker(pos);
  42. map.addOverlay(marker);
  43. if (html != null) {
  44. addMarkerPopup(marker, html);
  45. }
  46. }
  47. }
  48. }
  49. if (uidl.hasAttribute("width")) {
  50. setWidth("" + uidl.getIntAttribute("width"));
  51. }
  52. if (uidl.hasAttribute("height")) {
  53. setHeight("" + uidl.getIntAttribute("height"));
  54. }
  55. if (uidl.hasAttribute("zoom")) {
  56. map.setZoom(uidl.getIntAttribute("zoom"));
  57. }
  58. if (uidl.hasAttribute("centerX") && uidl.hasAttribute("centerY")) {
  59. final GLatLng center = new GLatLng(uidl
  60. .getDoubleAttribute("centerX"), uidl
  61. .getDoubleAttribute("centerY"));
  62. map.setCenter(center);
  63. } else if (pos != null) {
  64. // use last marker position
  65. map.setCenter(pos);
  66. }
  67. }
  68. private void addMarkerPopup(GMarker marker, String html) {
  69. if (markerEventManager == null) {
  70. markerEventManager = GMarkerEventManager.getInstance();
  71. }
  72. markerEventManager.addOnClickListener(marker, new MarkerEventListener(
  73. html));
  74. }
  75. private class MarkerEventListener implements GMarkerEventClickListener {
  76. String html;
  77. public MarkerEventListener(String html) {
  78. this.html = html;
  79. }
  80. public void onClick(GMarker marker) {
  81. marker.openInfoWindowHtml(html);
  82. }
  83. public void onDblClick(GMarker marker) {
  84. }
  85. }
  86. }