Class GoogleMap

java.lang.Object
com.vaadin.flow.component.Component
com.flowingcode.vaadin.addons.googlemaps.GoogleMap
All Implemented Interfaces:
AttachNotifier, DetachNotifier, HasElement, HasSize, Serializable

@Tag("google-map") @JsModule("@flowingcode/google-map/google-map.js") @JsModule("./googlemaps/geolocation.js") @NpmPackage(value="@flowingcode/google-map",version="3.9.0") @NpmPackage(value="@googlemaps/markerclusterer",version="2.0.8") public class GoogleMap extends Component implements HasSize
See Also:
  • Constructor Details

    • GoogleMap

      public GoogleMap(String apiKey, String clientId, String language)
      Initiates a new GoogleMap object.
      Parameters:
      apiKey - The Maps API key from Google. Not required when developing in localhost or when using a client id. Use null or empty string to disable.
      clientId - Google Maps API for Work client ID. Use this instead of API key if available. Use null or empty string to disable.
      language - The language to use with maps. See https://developers.google.com/maps/faq#languagesupport for the list of the supported languages. Use null or empty string to disable.
  • Method Details

    • getLatitude

      @Synchronize("google-map-idle") public Double getLatitude()
    • getLongitude

      @Synchronize("google-map-idle") public Double getLongitude()
    • setCenter

      public void setCenter(LatLon center)
      Sets the center of the map to the given coordinates.
      Parameters:
      center - The new coordinates of the center.
    • getCenter

      public LatLon getCenter()
      Returns the current position of the center of the map.
      Returns:
      Coordinates of the center.
    • setZoom

      public void setZoom(int zoom)
      Zooms the map to the given value.
      Parameters:
      zoom - New amount of the zoom.
    • getZoom

      @Synchronize("google-map-idle") public int getZoom()
      Returns the current zoom of the map.
      Returns:
      Current value of the zoom.
    • addMarker

      public GoogleMapMarker addMarker(String caption, LatLon position, boolean draggable, String iconUrl)
      Adds a new marker to the map.
      Parameters:
      caption - Caption of the marker shown when the marker is hovered.
      position - Coordinates of the marker on the map.
      draggable - Set true to enable dragging of the marker.
      iconUrl - The url of the icon of the marker.
      Returns:
      GoogleMapMarker object created with the given settings.
    • addMarker

      public GoogleMapMarker addMarker(String caption, LatLon position, boolean draggable, GoogleMapIcon icon)
      Adds a new marker to the map.
      Parameters:
      caption - Caption of the marker shown when the marker is hovered.
      position - Coordinates of the marker on the map.
      draggable - Set true to enable dragging of the marker.
      icon - the icon image of the marker.
      Returns:
      GoogleMapMarker object created with the given settings.
    • addPolygon

      public GoogleMapPolygon addPolygon(List<GoogleMapPoint> points)
      Creates a polygon with the given points and adds the new polygon to the map.
      Parameters:
      points - the points of the polygon
      Returns:
      the new created polygon
    • addPolygon

      public void addPolygon(GoogleMapPolygon polygon)
      Adds a new polygon to the map.
      Parameters:
      polygon - the polygon to be added to the map
    • removePolygon

      public void removePolygon(GoogleMapPolygon polygon)
      Removes a polygon from the map.
      Parameters:
      polygon - the polygon to be removed from the map
    • addPolyline

      public GoogleMapPolyline addPolyline(List<GoogleMapPoint> points)
      Creates a polyline with the given points and adds the new polyline to the map.
      Parameters:
      points - the points of the polyline
      Returns:
      the new created polyline
    • addPolyline

      public void addPolyline(GoogleMapPolyline polyline)
      Adds a new polyline to the map.
      Parameters:
      polyline - the polyline to be added to the map
    • removePolyline

      public void removePolyline(GoogleMapPolyline polyline)
      Removes a polyline from the map.
      Parameters:
      polyline - the polyline to be removed from the map
    • addMarker

      public void addMarker(GoogleMapMarker marker)
      Adds a marker to the map.
      Parameters:
      marker - The marker to add.
    • removeMarker

      public void removeMarker(GoogleMapMarker marker)
    • setMapType

      public void setMapType(GoogleMap.MapType type)
      Sets the type of the base map.
      Parameters:
      type - The new MapType to use.
    • getMapType

      public GoogleMap.MapType getMapType()
      Returns the current type of the base map.
      Returns:
      The current MapType.
    • isDraggable

      public boolean isDraggable()
      Checks if the map is currently draggable.
      Returns:
      true, if the map draggable.
    • setDraggable

      public void setDraggable(boolean draggable)
      Enables/disables dragging of the map.
      Parameters:
      draggable - Set to true to enable dragging.
    • setMaxZoom

      public void setMaxZoom(int maxZoom)
      Sets the maximum allowed amount of zoom (default 21.0).
      Parameters:
      maxZoom - The maximum amount for zoom.
    • getMaxZoom

      public int getMaxZoom()
      Returns the current maximum amount of zoom.
      Returns:
      maximum amount of zoom
    • setMinZoom

      public void setMinZoom(int minZoom)
      Sets the minimum allowed amount of zoom (default 0.0).
      Parameters:
      minZoom - The minimum amount for zoom.
    • getMinZoom

      public int getMinZoom()
      Returns the current minimum amount of zoom.
      Returns:
      minimum amount of zoom
    • disableRotateControl

      public void disableRotateControl(boolean disable)
      Allows to disable/enable rotate control.
      Parameters:
      disable - Set true to disable rotate control
    • disableStreetViewControl

      public void disableStreetViewControl(boolean disable)
      Allows to disable/enable street view control.
      Parameters:
      disable - Set true to disable street view control
    • disableMapTypeControl

      public void disableMapTypeControl(boolean disable)
      Allows to disable/enable map type control.
      Parameters:
      disable - Set true to disable map type control
    • disableZoomControl

      public void disableZoomControl(boolean disable)
      Allows to disable/enable zoom control.
      Parameters:
      disable - Set true to disable zoom control
    • disableFullScreenControl

      public void disableFullScreenControl(boolean disable)
      Allows to disable/enable full screen control.
      Parameters:
      disable - Set true to disable full screen control
    • disableScaleControl

      public void disableScaleControl(boolean disable)
      Allows to disable/enable scale control.
      Parameters:
      disable - Set true to disable scale control
    • setMapId

      public void setMapId(String mapId)
      Sets the mapId. See https://developers.google.com/maps/documentation/javascript/cloud-based-map-styling
      Parameters:
      mapId - If set, the custom style associated with that map id is applied.
    • getMapId

      public String getMapId()
      Returns the current map id.
      Returns:
      The current map id.
    • setControlSize

      public void setControlSize(int controlSize)
      Sets the size of the control buttons appearing in the map. Must be specified when creating the map. See https://developers.google.com/maps/documentation/javascript/reference/map#MapOptions.controlSize
      Parameters:
      controlSize - If set, control's size will be updated with this value.
    • getControlSize

      public int getControlSize()
      Returns the current control size value.
      Returns:
      The current size of the control buttons.
    • setKml

      public void setKml(String kml)
      Sets a KML or GeoRSS feed url to be displayed as a KML Layer in the map.
      Parameters:
      kml - the url to be displayed.
    • getKml

      public String getKml()
      Returns the current KML or GeoRSS feed url associated with a KML Layer in the map.
      Returns:
      the current url.
    • enableMarkersClustering

      public void enableMarkersClustering()
      Enables markers clustering.
    • setTilt

      public void setTilt(double tilt)
      Sets tilt value on a vector map.
      Parameters:
      tilt - the value to set
    • getTilt

      public double getTilt()
      Returns current tilt value.
      Returns:
      tilt value
    • setHeading

      public void setHeading(double heading)
      Sets heading (rotation) value on a vector map.
      Parameters:
      heading - the value to set
    • getHeading

      public double getHeading()
      Returns the current heading value.
      Returns:
      haeading value
    • addClickListener

    • addRightClickListener

      public Registration addRightClickListener(ComponentEventListener<GoogleMap.GoogleMapClickEvent> listener)
    • goToCurrentLocation

      public void goToCurrentLocation()
      Sets current location on map using default geolocation options.

      Setting geolocation requires that the user gives consent to location sharing when prompted by the browser.

    • goToCurrentLocation

      public void goToCurrentLocation(GeolocationOptions options)
      Sets current location on map using specific geolocation options.

      Setting geolocation requires that the user gives consent to location sharing when prompted by the browser.

      Parameters:
      options - the geolocation options (enableHighAccuracy, timeout, maximumAge)
    • addCurrentLocationEventListener

      public Registration addCurrentLocationEventListener(ComponentEventListener<GoogleMap.CurrentLocationEvent> listener)
      Adds a CurrentLocationEvent listener. The listener is called when setting the current location.
      Parameters:
      listener -
      Returns:
      a handle that can be used for removing the listener
    • addGeolocationErrorEventListener

      public Registration addGeolocationErrorEventListener(ComponentEventListener<GoogleMap.GeolocationErrorEvent> listener)
      Adds a GeolocationErrorEvent listener. The listener is called when current location can't be found.
      Parameters:
      listener -
      Returns:
      a handle that can be used for removing the listener
    • trackLocation

      public void trackLocation()
      Activates tracking current location on map using default geolocation options.

      Uses geolocation#watchPosition method to track current position.

      Geolocation requires that the user gives consent to location sharing when prompted by the browser.

      Throws:
      IllegalStateException - if a tracking location session is already active. The current session must be stopped before starting a new one.
    • trackLocation

      public void trackLocation(GeolocationOptions options)
      Activates tracking current location on map.

      Uses geolocation#watchPosition method to track current position.

      Geolocation requires that the user gives consent to location sharing when prompted by the browser.

      Parameters:
      options - the geolocation options (enableHighAccuracy, timeout, maximumAge)
      Throws:
      IllegalStateException - if a tracking location session is already active. The current session must be stopped before starting a new one.
    • getTrackLocationId

      public Integer getTrackLocationId()
      Returns track location id if a location tracking was activated.
      Returns:
      the location tracking id
    • addLocationTrackingActivatedEventListener

      public Registration addLocationTrackingActivatedEventListener(ComponentEventListener<GoogleMap.LocationTrackingActivatedEvent> listener)
      Adds a LocationTrackingActivatedEvent listener. The listener is called when setting activating tracking location.
      Parameters:
      listener - a listener for a LocationTrackingActivatedEvent
      Returns:
      a handle for the listener
    • stopTrackLocation

      public void stopTrackLocation()
      Stops the current location tracking session.
    • getBounds

      public CompletableFuture<LatLonBounds> getBounds()
      Returns a CompletableFuture containing the map current bounds.
      Returns:
      current bounds
    • setClusteringRenderer

      public void setClusteringRenderer(String customRenderer)
      Sets the custom renderer definition to be applied to the markers clustering. The custom renderer needs to be define as a global JavaScript object conforming the Renderer interface from https://github.com/googlemaps/js-markerclusterer/blob/5ac92567dd0c52a1e1b897d791463a064656830c/src/renderer.ts#L65C2-L65C78
      Parameters:
      customRenderer - the custom renderer definition
    • addMapIdleListener

      public Registration addMapIdleListener(ComponentEventListener<GoogleMap.GoogleMapIdleEvent> listener)
      Adds a GoogleMapIdleEvent listener. The listener is called when the map is in idle state (after pan or zoom)
      Parameters:
      listener -
      Returns:
      a handle that can be used for removing the listener
    • addGoogleMapBoundsChangedListener

      public Registration addGoogleMapBoundsChangedListener(ComponentEventListener<GoogleMap.GoogleMapBoundsChangedEvent> listener)
      Adds a GoogleMapBoundsChangedEvent listener. The listener is called when the map is moved or zoomed
      Parameters:
      listener -
      Returns:
      a handle that can be used for removing the listener
    • addCustomControls

      @Deprecated public void addCustomControls(CustomControl... customControls)
      Deprecated.
      Adds custom control buttons to the map.
      Parameters:
      customControls - list of custom controls to add to the map
    • setCustomControls

      public void setCustomControls(CustomControl... customControls)
      Sets the custom control buttons to be displayed in the map.
      Parameters:
      customControls - list of custom controls to add to the map
    • addCustomControl

      public void addCustomControl(CustomControl customControl)
      Adds a custom control to be displayed in the map.
      Parameters:
      customControl - the custom control to add to the map
    • removeCustomControl

      public void removeCustomControl(CustomControl customControl)
      Removes a custom control added to the map.
      Parameters:
      customControl - the custom control to be removed
    • removeCustomControls

      public void removeCustomControls()
      Removes all custom controls added to the map.
    • addFullScreenListener

      public Registration addFullScreenListener(ComponentEventListener<GoogleMap.FullScreenEvent> listener)
      Adds a FullScreenEvent listener. The listener is called to notify whether the map is in full screen mode.
      Parameters:
      listener - a FullScreenEvent listener
      Returns:
      Registration object to allow removing the listener
    • closeFullScreen

      public void closeFullScreen()
      Exits the full screen mode on the map.
    • setMapStyle

      public void setMapStyle(MapStyle... mapStyles)
      Sets style formatting to the features and elements of the map.
      Parameters:
      mapStyles - formatting to be applied to the map features and elements