public class MapView

  1. Object
  2. Component
  3. Container
  4. MapView

ImplementsAnimation, Editable, Iterable<Component>, MapSurface, StyleListener

A pure-vector map component: it renders entirely through the Codename One Graphics API (the built-in VectorMapEngine) and never embeds a native peer, so it composes cleanly with the rest of the UI – dialogs, lists and overlays draw over it without the clipping limitations of a native view.

MapView works identically on every platform including the simulator and the web. By default it shows the free, keyless OpenFreeMap vector basemap (real OpenStreetMap data) so it renders real maps with zero configuration and no API key; point it at any other TileSource (a keyed MVT endpoint, a raster source such as RasterTileSource.openStreetMap(), or a bundled offline tileset) as needed. For a native-rendered map (Apple MapKit, Google Maps, …) use NativeMap, which falls back to this component when no native provider is wired in.

Constructors

public MapView()Creates a map showing the free, keyless OpenFreeMap vector basemap (real OpenStreetMap data) centered on the equator at a low zoom.
public MapView(TileSource source)Creates a map backed by source with the default light style.
public MapView(TileSource source, MapStyle style)Creates a map backed by source and styled by style (the style is only consulted for vector sources).

Methods

public boolean isLoadingTiles()Whether the vector engine still has tiles in flight for the current view.
public boolean isMapReady()Whether every tile visible in the current viewport has finished loading/decoding/rendering.
public VectorMapEngine getEngine()The underlying vector engine, for advanced configuration (tile cache, source and style swapping).
public MapView setTileSource(TileSource source)Replaces the tile source.
public MapView setStyle(MapStyle style)Replaces the style.
public CameraPosition getCameraPosition()The current camera position (target, zoom, bearing, tilt).
public void setCameraPosition(CameraPosition position)Moves the camera to position, animating where the backend supports it.
public void moveCamera(LatLng target, double zoom)Convenience to recenter at target and set zoom in one call.
public double getZoom()The current zoom level.
public void setZoom(double zoom)Sets the zoom level, keeping the current center.
public double getMinZoom()The smallest zoom level the backend permits.
public double getMaxZoom()The largest zoom level the backend permits.
public LatLng getCenter()The geographic coordinate at the center of the viewport.
public void setCenter(LatLng center)Recenters the viewport at center, keeping the current zoom.
public MapBounds getVisibleRegion()The geographic bounds currently visible, or null before layout.
public void fitBounds(MapBounds bounds, int paddingPixels)Moves and zooms the camera so bounds fits within the viewport, inset by paddingPixels on every edge.
public Marker addMarker(MarkerOptions options)Adds a marker described by options and returns its live handle.
public void removeMarker(Marker marker)Removes a previously added marker.
public Polyline addPolyline(Polyline polyline)Adds a polyline and returns it for chaining.
public void removePolyline(Polyline polyline)Removes a previously added polyline.
public Polygon addPolygon(Polygon polygon)Adds a polygon and returns it for chaining.
public void removePolygon(Polygon polygon)Removes a previously added polygon.
public Circle addCircle(Circle circle)Adds a circle and returns it for chaining.
public void removeCircle(Circle circle)Removes a previously added circle.
public void clearMapObjects()Removes every marker, polyline, polygon and circle.
public Point latLngToScreen(LatLng coord)Converts a geographic coordinate to a pixel relative to this component.
public LatLng screenToLatLng(int x, int y)Converts a pixel relative to this component to a geographic coordinate.
public void addTapListener(MapTapListener l)Registers a tap listener.
public void removeTapListener(MapTapListener l)Unregisters a tap listener.
public void addLongPressListener(MapTapListener l)Registers a long-press listener.
public void removeLongPressListener(MapTapListener l)Unregisters a long-press listener.
public void addCameraChangeListener(CameraChangeListener l)Registers a camera-change listener.
public void removeCameraChangeListener(CameraChangeListener l)Unregisters a camera-change listener.
public boolean isNativeMap()True when a native provider currently backs this surface; false for a pure-vector map or a NativeMap that fell back to the vector engine.
public Component asComponent()This surface as a Codename One Component for layout purposes.
protected void paintBackground(Graphics g)This method paints the Component background, it should be overriden by subclasses to perform custom background drawing.
public void pointerPressed(int x, int y)If this Component is focused, the pointer pressed event will call this method
protected boolean mouseWheel(WheelEvent ev)Handles a scroll wheel or trackpad scroll over this component, before its listeners and before anything above it in the hierarchy.
public void pointerDragged(int x, int y)If this Component is focused, the pointer dragged event will call this method
public void pointerReleased(int x, int y)If this Component is focused, the pointer released event will call this method
public void longPointerPress(int x, int y)If this Component is focused this method is invoked when the user presses and holds the pointer on the Component
protected boolean pinch(float scale)Invoked by subclasses interested in handling pinch to zoom events, if true is returned other drag events will not be broadcast
protected Dimension calcPreferredSize()Calculates the preferred size based on component content.

Inherited fields

Inherited methods

From Container

encloseIn, encloseIn, initLaf, getUIManager, setUIManager, isSurface, add, addAll, add, add, add, add, add, getLeadComponent, setLeadComponent, getLeadParent, keyPressed, keyReleased, getLayout, setLayout, invalidate, setShouldLayout, setShouldCalcPreferredSize, getLayoutWidth, getLayoutHeight, applyRTL, constrainWidthWhenScrollable, constrainHeightWhenScrollable, addComponent, addComponent, addComponent, addComponent, replaceAndWait, replaceAndWait, replace, replaceAndWait, replace, createReplaceTransition, isEnabled, setEnabled, removeComponent, cancelRepaints, flushReplace, removeAll, revalidateWithAnimationSafety, revalidate, revalidateLater, forceRevalidate, clearClientProperties, paint, paintGlass, layoutContainer, isSafeArea, setSafeArea, isSafeAreaRoot, getSafeAreaRoot, setSafeAreaRoot, getComponentCount, getComponentAt, getComponentIndex, contains, scrollComponentToVisible, getClosestComponentTo, getResponderAt, getComponentAt, findDropTargetAt, paramString, refreshTheme, isScrollableX, setScrollableX, isScrollableY, setScrollableY, getSideGap, getBottomGap, setScrollable, setCellRenderer, getScrollIncrement, setScrollIncrement, findFirstFocusable, dragInitiated, fireClicked, isSelectableInteraction, getGridPosY, paintComponentBackground, getGridPosX, animateHierarchyAndWait, createAnimateHierarchy, animateHierarchy, animateHierarchyFadeAndWait, createAnimateHierarchyFade, animateHierarchyFade, animateLayoutFadeAndWait, createAnimateLayoutFadeAndWait, animateLayoutFade, createAnimateLayoutFade, animateLayoutAndWait, animateLayout, updateTabIndices, createAnimateLayout, drop, createAnimateMotion, morph, morphAndWait, animateUnlayout, animateUnlayoutAndWait, createAnimateUnlayout, getChildrenAsList, iterator, iterator

From Component

setSameSize, isSetCursorSupported, parsePreferredSize, getDefaultDragTransparency, setDefaultDragTransparency, getEditingDelegate, setEditingDelegate, getCursor, setCursor, showNativeOverlay, hideNativeOverlay, updateNativeOverlay, getNativeOverlay, getAllStyles, getSameWidth, setSameWidth, getSameHeight, setSameHeight, getX, setX, getOuterX, getInnerX, getY, setY, getOuterY, getInnerY, isVisible, setVisible, getClientProperty, stripMarginAndPadding, putClientProperty, getDirtyRegion, setDirtyRegion, isOpaque, setOpaque, getWidth, setWidth, getOuterWidth, getInnerWidth, getHeight, setHeight, getOuterHeight, getInnerHeight, isDragRegion, getDragRegionStatus, getBaseline, getBaselineResizeBehavior, getPreferredSizeStr, setPreferredSizeStr, getPreferredSize, setPreferredSize, getScrollDimension, calcScrollSize, setScrollSize, getPreferredW, setPreferredW, getPreferredH, setPreferredH, getOuterPreferredH, getInnerPreferredH, getOuterPreferredW, getInnerPreferredW, setSize, getUIID, setUIID, setUIIDFinal, setUIID, getInlineAllStyles, setInlineAllStyles, getInlineSelectedStyles, setInlineSelectedStyles, getInlineUnselectedStyles, setInlineUnselectedStyles, getInlineDisabledStyles, setInlineDisabledStyles, getInlinePressedStyles, setInlinePressedStyles, remove, getParent, getOwner, setOwner, isOwnedBy, containsOrOwns, addFocusListener, removeFocusListener, addScrollListener, removeScrollListener, getSelectCommandText, setSelectCommandText, getLabelForComponent, setLabelForComponent, focusGained, focusLost, paintBackgrounds, paintShadows, getAbsoluteX, getAbsoluteY, isInClippingRegion, paintIntersectingComponentsAbove, paintScrollbars, paintScrollbarX, getScrollOpacity, getSelectedRect, paintScrollbarY, paintComponent, paintComponent, getBorder, getScrollable, isScrollable, getScrollX, setScrollX, getScrollY, setScrollY, onScrollX, onScrollY, getDraggedx, getDraggedy, contains, visibleBoundsContains, hasFixedPreferredSize, getBounds, getBounds, getVisibleBounds, getVisibleBounds, isFocusable, setFocusable, onSetFocusable, resetFocusable, getTabIndex, setTabIndex, getPreferredTabIndex, setPreferredTabIndex, isTraversable, setTraversable, handlesInput, setHandlesInput, consumesRawTextInput, hasFocus, setFocus, getComponentForm, getTopLevelContainer, repaint, repaint, longKeyPress, keyRepeated, registerForAnimation, deregisterFromAnimation, getAnimationManager, getScrollAnimationSpeed, setScrollAnimationSpeed, isBlockLead, setBlockLead, isIgnorePointerEvents, setIgnorePointerEvents, isRippleEffect, setRippleEffect, getInlineStylesTheme, setInlineStylesTheme, shouldRenderComponentSelection, isHideInLandscape, setHideInLandscape, createStyleAnimation, isSmoothScrolling, setSmoothScrolling, pointerHover, stopScrollMomentum, pointerHoverReleased, pointerHoverPressed, pinchReleased, pinch, rotation, isPinchBlocksDragAndDrop, setPinchBlocksDragAndDrop, pointerDragged, getDragImage, getDragTransparency, setDragTransparency, toImage, drawDraggedImage, draggingOver, dragEnter, dragExit, addPullToRefresh, setPullToRefresh, respondsToPointerEvents, isStickyDrag, pointerPressed, isDragAndDropOperation, pointerReleased, setVerticalScrollBounds, setHorizontalScrollBounds, isVScrollThumbGrabbed, isHScrollThumbGrabbed, isVScrollThumbHover, isHScrollThumbHover, isTensileDragEnabled, setTensileDragEnabled, getTextSelectionSupport, addDropListener, removeDropListener, addDragOverListener, removeDragOverListener, isNativeDragSource, setNativeDragSource, getNativeDragOperation, setNativeDragOperation, createNativeDragOperation, isNativeDropTarget, setNativeDropTarget, getAcceptedDropMimeTypes, setAcceptedDropMimeTypes, getAcceptedDropActions, setAcceptedDropActions, canAcceptNativeDrop, nativeDragEnter, nativeDragOver, nativeDragExit, nativeDrop, addNativeDropListener, removeNativeDropListener, addNativeDragOverListener, removeNativeDragOverListener, dragFinished, addDragFinishedListener, addStateChangeListener, removeStateChangeListener, addPointerPressedListener, addLongPressListener, addContextMenuListener, removeContextMenuListener, addMouseWheelListener, removeMouseWheelListener, addStylusListener, removeStylusListener, paintRippleOverlay, removePointerPressedListener, removeLongPressListener, removeDragFinishedListener, addPointerReleasedListener, removePointerReleasedListener, addPointerDraggedListener, removePointerDraggedListener, getDragSpeed, getStyle, getPressedStyle, setPressedStyle, initUnselectedStyle, initPressedStyle, initDisabledStyle, initSelectedStyle, getUnselectedStyle, setUnselectedStyle, getSelectedStyle, setSelectedStyle, getDisabledStyle, setDisabledStyle, installDefaultPainter, requestFocus, toString, refreshTheme, refreshTheme, isDragActivated, animate, scrollRectToVisible, scrollRectToVisible, paintBorder, paintBorderBackground, isCellRenderer, isScrollVisible, setScrollVisible, setIsScrollVisible, startEditingAsync, stopEditing, isEditing, isEditable, laidOut, deinitialize, initComponent, isInitialized, setInitialized, styleChanged, getNextFocusDown, setNextFocusDown, getNextFocusUp, setNextFocusUp, getNextFocusLeft, setNextFocusLeft, getNextFocusRight, setNextFocusRight, getName, setName, initCustomStyle, deinitializeCustomStyle, isRTL, setRTL, isTactileTouch, isTactileTouch, setTactileTouch, getPropertyNames, getPropertyTypes, getPropertyTypeNames, getPropertyValue, setPropertyValue, paintLockRelease, paintLock, isSnapToGrid, setSnapToGrid, shouldBlockSideSwipe, shouldBlockSideSwipeLeft, shouldBlockSideSwipeRight, blocksSideSwipe, isFlatten, setFlatten, getTensileLength, setTensileLength, isGrabsPointerEvents, setGrabsPointerEvents, getScrollOpacityChangeSpeed, setScrollOpacityChangeSpeed, growShrink, isAlwaysTensile, setAlwaysTensile, isDraggable, setDraggable, isDropTarget, setDropTarget, isChildOf, isHideInPortrait, setHideInPortrait, getBindablePropertyNames, getBindablePropertyTypes, bindProperty, unbindProperty, getBoundPropertyValue, setBoundPropertyValue, getCloudBoundProperty, setCloudBoundProperty, getCloudDestinationProperty, setCloudDestinationProperty, getComponentState, setComponentState, setHidden, isHidden, setHidden, isHidden, announceForAccessibility, getAccessibilityText, setAccessibilityText, getSemantics, getAccessibilityNode, accessibilityChanged, accessibilityChanged, getTooltip, setTooltip

Constructor details

MapView

public MapView()
Creates a map showing the free, keyless OpenFreeMap vector basemap (real OpenStreetMap data) centered on the equator at a low zoom.

MapView

public MapView(TileSource source)
Creates a map backed by source with the default light style.

MapView

public MapView(TileSource source, MapStyle style)
Creates a map backed by source and styled by style (the style is only consulted for vector sources).

Method details

isLoadingTiles

public boolean isLoadingTiles()
Whether the vector engine still has tiles in flight for the current view. Useful to defer a screenshot (or hide a spinner) until the basemap has finished loading. Always false for a native-provider-backed surface, which loads its own tiles outside the engine’s knowledge.

isMapReady

public boolean isMapReady()
Whether every tile visible in the current viewport has finished loading/decoding/rendering. Unlike isLoadingTiles(), this actively computes the visible tile set and requests missing tiles, so it is a deterministic readiness probe even before the first paint has run.

getEngine

public VectorMapEngine getEngine()
The underlying vector engine, for advanced configuration (tile cache, source and style swapping).

setTileSource

public MapView setTileSource(TileSource source)
Replaces the tile source.

setStyle

public MapView setStyle(MapStyle style)
Replaces the style.

getCameraPosition

public CameraPosition getCameraPosition()
The current camera position (target, zoom, bearing, tilt).

setCameraPosition

public void setCameraPosition(CameraPosition position)
Moves the camera to position, animating where the backend supports it.

moveCamera

public void moveCamera(LatLng target, double zoom)
Convenience to recenter at target and set zoom in one call.

getZoom

public double getZoom()
The current zoom level.

setZoom

public void setZoom(double zoom)
Sets the zoom level, keeping the current center.

getMinZoom

public double getMinZoom()
The smallest zoom level the backend permits.

getMaxZoom

public double getMaxZoom()
The largest zoom level the backend permits.

getCenter

public LatLng getCenter()
The geographic coordinate at the center of the viewport.

setCenter

public void setCenter(LatLng center)
Recenters the viewport at center, keeping the current zoom.

getVisibleRegion

public MapBounds getVisibleRegion()
The geographic bounds currently visible, or null before layout. (Named getVisibleRegion to avoid clashing with Component.getVisibleBounds(), which returns a pixel rectangle.)

fitBounds

public void fitBounds(MapBounds bounds, int paddingPixels)
Moves and zooms the camera so bounds fits within the viewport, inset by paddingPixels on every edge.

addMarker

public Marker addMarker(MarkerOptions options)
Adds a marker described by options and returns its live handle.

removeMarker

public void removeMarker(Marker marker)
Removes a previously added marker.

addPolyline

public Polyline addPolyline(Polyline polyline)
Adds a polyline and returns it for chaining.

removePolyline

public void removePolyline(Polyline polyline)
Removes a previously added polyline.

addPolygon

public Polygon addPolygon(Polygon polygon)
Adds a polygon and returns it for chaining.

removePolygon

public void removePolygon(Polygon polygon)
Removes a previously added polygon.

addCircle

public Circle addCircle(Circle circle)
Adds a circle and returns it for chaining.

removeCircle

public void removeCircle(Circle circle)
Removes a previously added circle.

clearMapObjects

public void clearMapObjects()
Removes every marker, polyline, polygon and circle.

latLngToScreen

public Point latLngToScreen(LatLng coord)
Converts a geographic coordinate to a pixel relative to this component.

screenToLatLng

public LatLng screenToLatLng(int x, int y)
Converts a pixel relative to this component to a geographic coordinate.

addTapListener

public void addTapListener(MapTapListener l)
Registers a tap listener.

removeTapListener

public void removeTapListener(MapTapListener l)
Unregisters a tap listener.

addLongPressListener

public void addLongPressListener(MapTapListener l)
Registers a long-press listener.

removeLongPressListener

public void removeLongPressListener(MapTapListener l)
Unregisters a long-press listener.

addCameraChangeListener

public void addCameraChangeListener(CameraChangeListener l)
Registers a camera-change listener.

removeCameraChangeListener

public void removeCameraChangeListener(CameraChangeListener l)
Unregisters a camera-change listener.

isNativeMap

public boolean isNativeMap()
True when a native provider currently backs this surface; false for a pure-vector map or a NativeMap that fell back to the vector engine.

asComponent

public Component asComponent()
This surface as a Codename One Component for layout purposes.

paintBackground

protected void paintBackground(Graphics g)
This method paints the Component background, it should be overriden by subclasses to perform custom background drawing.

Parameters

g Graphics
the component graphics

pointerPressed

public void pointerPressed(int x, int y)
If this Component is focused, the pointer pressed event will call this method

Parameters

x int
the pointer x coordinate
y int
the pointer y coordinate

mouseWheel

protected boolean mouseWheel(WheelEvent ev)

Handles a scroll wheel or trackpad scroll over this component, before its listeners and before anything above it in the hierarchy.

A component that moves its own content – an editor that scrolls itself, a viewer that pans – implements this. Everything else leaves it alone and the wheel scrolls the nearest scrollable ancestor, which is what a wheel means.

This exists because a wheel used to arrive as a synthetic press, drag and release played into the component tree: a component that wanted the wheel got it by handling pointer events, and so did every component that did not want it. The events are gone and this is what replaces them, for the few components that have something of their own to move.

A wheel pans the camera, and tells the listeners about it – which is the half a synthetic drag could never deliver, because the gesture it emulated had no end that meant “the pan is finished”.

Parameters

ev WheelEvent
the wheel event, carrying the scroll deltas in display pixels

Returns

true when this component handled the wheel and nothing else should act on it

pointerDragged

public void pointerDragged(int x, int y)
If this Component is focused, the pointer dragged event will call this method

Parameters

x int
the pointer x coordinate
y int
the pointer y coordinate

pointerReleased

public void pointerReleased(int x, int y)
If this Component is focused, the pointer released event will call this method

Parameters

x int
the pointer x coordinate
y int
the pointer y coordinate

longPointerPress

public void longPointerPress(int x, int y)
If this Component is focused this method is invoked when the user presses and holds the pointer on the Component

pinch

protected boolean pinch(float scale)
Invoked by subclasses interested in handling pinch to zoom events, if true is returned other drag events will not be broadcast

Parameters

scale float
the scaling of the pinch operation a number larger than 1 means scaling up and smaller than 1 means scaling down. It is recommended that code would threshold the number (so a change between 1.0 and 1.02 shouldn’t necessarily trigger zoom). Notice that this number is relevant to current zoom levels and unaware of them so you should also enforce limits of maximum/minimum zoom levels.

Returns

false by default

calcPreferredSize

protected Dimension calcPreferredSize()
Calculates the preferred size based on component content. This method is invoked lazily by getPreferred size.

Returns

the calculated preferred size based on component content