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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassEvent that is fired when setting current location on map.static classEvent fired when the full screen mode changes on the map.classEvent that is called when current location can't be found.static classstatic classstatic classclassEvent that is fired when activating location tracking.static enumBase map types supported by Google Maps. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdds a CurrentLocationEvent listener.voidaddCustomControl(CustomControl customControl) Adds a custom control to be displayed in the map.voidaddCustomControls(CustomControl... customControls) Deprecated.Adds a FullScreenEvent listener.Adds a GeolocationErrorEvent listener.addGoogleMapBoundsChangedListener(ComponentEventListener<GoogleMap.GoogleMapBoundsChangedEvent> listener) Adds a GoogleMapBoundsChangedEvent listener.addLocationTrackingActivatedEventListener(ComponentEventListener<GoogleMap.LocationTrackingActivatedEvent> listener) Adds a LocationTrackingActivatedEvent listener.Adds a GoogleMapIdleEvent listener.voidaddMarker(GoogleMapMarker marker) Adds a marker to the map.addMarker(String caption, LatLon position, boolean draggable, GoogleMapIcon icon) Adds a new marker to the map.Adds a new marker to the map.voidaddPolygon(GoogleMapPolygon polygon) Adds a new polygon to the map.addPolygon(List<GoogleMapPoint> points) Creates a polygon with the given points and adds the new polygon to the map.voidaddPolyline(GoogleMapPolyline polyline) Adds a new polyline to the map.addPolyline(List<GoogleMapPoint> points) Creates a polyline with the given points and adds the new polyline to the map.voidExits the full screen mode on the map.voiddisableFullScreenControl(boolean disable) Allows to disable/enable full screen control.voiddisableMapTypeControl(boolean disable) Allows to disable/enable map type control.voiddisableRotateControl(boolean disable) Allows to disable/enable rotate control.voiddisableScaleControl(boolean disable) Allows to disable/enable scale control.voiddisableStreetViewControl(boolean disable) Allows to disable/enable street view control.voiddisableZoomControl(boolean disable) Allows to disable/enable zoom control.voidEnables markers clustering.Returns aCompletableFuturecontaining the map currentbounds.Returns the current position of the center of the map.intReturns the current control size value.doubleReturns the current heading value.getKml()Returns the current KML or GeoRSS feed url associated with a KML Layer in the map.getMapId()Returns the current map id.Returns the current type of the base map.intReturns the current maximum amount of zoom.intReturns the current minimum amount of zoom.doublegetTilt()Returns current tilt value.Returns track location id if a location tracking was activated.intgetZoom()Returns the current zoom of the map.voidSets current location on map using default geolocation options.voidgoToCurrentLocation(GeolocationOptions options) Sets current location on map using specific geolocation options.booleanChecks if the map is currently draggable.voidremoveCustomControl(CustomControl customControl) Removes a custom control added to the map.voidRemoves all custom controls added to the map.voidremoveMarker(GoogleMapMarker marker) voidremovePolygon(GoogleMapPolygon polygon) Removes a polygon from the map.voidremovePolyline(GoogleMapPolyline polyline) Removes a polyline from the map.voidSets the center of the map to the given coordinates.voidsetClusteringRenderer(String customRenderer) Sets the custom renderer definition to be applied to the markers clustering.voidsetControlSize(int controlSize) Sets the size of the control buttons appearing in the map.voidsetCustomControls(CustomControl... customControls) Sets the custom control buttons to be displayed in the map.voidsetDraggable(boolean draggable) Enables/disables dragging of the map.voidsetHeading(double heading) Sets heading (rotation) value on a vector map.voidSets a KML or GeoRSS feed url to be displayed as a KML Layer in the map.voidSets the mapId.voidsetMapStyle(MapStyle... mapStyles) voidsetMapType(GoogleMap.MapType type) Sets the type of the base map.voidsetMaxZoom(int maxZoom) Sets the maximum allowed amount of zoom (default 21.0).voidsetMinZoom(int minZoom) Sets the minimum allowed amount of zoom (default 0.0).voidsetTilt(double tilt) Sets tilt value on a vector map.voidsetZoom(int zoom) Zooms the map to the given value.voidStops the current location tracking session.voidActivates tracking current location on map using default geolocation options.voidtrackLocation(GeolocationOptions options) Activates tracking current location on map.Methods inherited from class com.vaadin.flow.component.Component
addListener, fireEvent, from, get, getChildren, getElement, getEventBus, getId, getLocale, getParent, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, onAttach, onDetach, onEnabledStateChanged, set, setElement, setId, setVisibleMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.vaadin.flow.component.AttachNotifier
addAttachListenerMethods inherited from interface com.vaadin.flow.component.DetachNotifier
addDetachListenerMethods inherited from interface com.vaadin.flow.component.HasElement
getElementMethods inherited from interface com.vaadin.flow.component.HasSize
getHeight, getHeightUnit, getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, getWidth, getWidthUnit, setHeight, setHeight, setHeightFull, setMaxHeight, setMaxHeight, setMaxWidth, setMaxWidth, setMinHeight, setMinHeight, setMinWidth, setMinWidth, setSizeFull, setSizeUndefined, setWidth, setWidth, setWidthFull
-
Constructor Details
-
GoogleMap
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
-
getLongitude
-
setCenter
Sets the center of the map to the given coordinates.- Parameters:
center- The new coordinates of the center.
-
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
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
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
Adds a new polygon to the map.- Parameters:
polygon- the polygon to be added to the map
-
removePolygon
Removes a polygon from the map.- Parameters:
polygon- the polygon to be removed from the map
-
addPolyline
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
Adds a new polyline to the map.- Parameters:
polyline- the polyline to be added to the map
-
removePolyline
Removes a polyline from the map.- Parameters:
polyline- the polyline to be removed from the map
-
addMarker
Adds a marker to the map.- Parameters:
marker- The marker to add.
-
removeMarker
-
setMapType
Sets the type of the base map.- Parameters:
type- The new MapType to use.
-
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
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
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
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
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
public Registration addClickListener(ComponentEventListener<GoogleMap.GoogleMapClickEvent> listener) -
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
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
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
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
Returns aCompletableFuturecontaining the map currentbounds.- Returns:
- current
bounds
-
setClusteringRenderer
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.setCustomControls(CustomControl...customControls)should be used instead.Adds custom control buttons to the map.- Parameters:
customControls- list of custom controls to add to the map
-
setCustomControls
Sets the custom control buttons to be displayed in the map.- Parameters:
customControls- list of custom controls to add to the map
-
addCustomControl
Adds a custom control to be displayed in the map.- Parameters:
customControl- the custom control to add to the map
-
removeCustomControl
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
- Parameters:
mapStyles- formatting to be applied to the map features and elements
-
setCustomControls(CustomControl...customControls)should be used instead.