Class MapView

All Implemented Interfaces:
MapSurface, Animation, Editable, StyleListener, Iterable<Component>

public class MapView extends Container implements MapSurface

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.

  • 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

    • 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).
      Specified by:
      getCameraPosition in interface MapSurface
    • setCameraPosition

      public void setCameraPosition(CameraPosition position)
      Moves the camera to position, animating where the backend supports it.
      Specified by:
      setCameraPosition in interface MapSurface
    • moveCamera

      public void moveCamera(LatLng target, double zoom)
      Convenience to recenter at target and set zoom in one call.
      Specified by:
      moveCamera in interface MapSurface
    • getZoom

      public double getZoom()
      The current zoom level.
      Specified by:
      getZoom in interface MapSurface
    • setZoom

      public void setZoom(double zoom)
      Sets the zoom level, keeping the current center.
      Specified by:
      setZoom in interface MapSurface
    • getMinZoom

      public double getMinZoom()
      The smallest zoom level the backend permits.
      Specified by:
      getMinZoom in interface MapSurface
    • getMaxZoom

      public double getMaxZoom()
      The largest zoom level the backend permits.
      Specified by:
      getMaxZoom in interface MapSurface
    • getCenter

      public LatLng getCenter()
      The geographic coordinate at the center of the viewport.
      Specified by:
      getCenter in interface MapSurface
    • setCenter

      public void setCenter(LatLng center)
      Recenters the viewport at center, keeping the current zoom.
      Specified by:
      setCenter in interface MapSurface
    • 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.)
      Specified by:
      getVisibleRegion in interface MapSurface
    • 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.
      Specified by:
      fitBounds in interface MapSurface
    • addMarker

      public Marker addMarker(MarkerOptions options)
      Adds a marker described by options and returns its live handle.
      Specified by:
      addMarker in interface MapSurface
    • removeMarker

      public void removeMarker(Marker marker)
      Removes a previously added marker.
      Specified by:
      removeMarker in interface MapSurface
    • addPolyline

      public Polyline addPolyline(Polyline polyline)
      Adds a polyline and returns it for chaining.
      Specified by:
      addPolyline in interface MapSurface
    • removePolyline

      public void removePolyline(Polyline polyline)
      Removes a previously added polyline.
      Specified by:
      removePolyline in interface MapSurface
    • addPolygon

      public Polygon addPolygon(Polygon polygon)
      Adds a polygon and returns it for chaining.
      Specified by:
      addPolygon in interface MapSurface
    • removePolygon

      public void removePolygon(Polygon polygon)
      Removes a previously added polygon.
      Specified by:
      removePolygon in interface MapSurface
    • addCircle

      public Circle addCircle(Circle circle)
      Adds a circle and returns it for chaining.
      Specified by:
      addCircle in interface MapSurface
    • removeCircle

      public void removeCircle(Circle circle)
      Removes a previously added circle.
      Specified by:
      removeCircle in interface MapSurface
    • clearMapObjects

      public void clearMapObjects()
      Removes every marker, polyline, polygon and circle.
      Specified by:
      clearMapObjects in interface MapSurface
    • latLngToScreen

      public Point latLngToScreen(LatLng coord)
      Converts a geographic coordinate to a pixel relative to this component.
      Specified by:
      latLngToScreen in interface MapSurface
    • screenToLatLng

      public LatLng screenToLatLng(int x, int y)
      Converts a pixel relative to this component to a geographic coordinate.
      Specified by:
      screenToLatLng in interface MapSurface
    • addTapListener

      public void addTapListener(MapTapListener l)
      Registers a tap listener.
      Specified by:
      addTapListener in interface MapSurface
    • removeTapListener

      public void removeTapListener(MapTapListener l)
      Unregisters a tap listener.
      Specified by:
      removeTapListener in interface MapSurface
    • addLongPressListener

      public void addLongPressListener(MapTapListener l)
      Registers a long-press listener.
      Specified by:
      addLongPressListener in interface MapSurface
    • removeLongPressListener

      public void removeLongPressListener(MapTapListener l)
      Unregisters a long-press listener.
      Specified by:
      removeLongPressListener in interface MapSurface
    • addCameraChangeListener

      public void addCameraChangeListener(CameraChangeListener l)
      Registers a camera-change listener.
      Specified by:
      addCameraChangeListener in interface MapSurface
    • removeCameraChangeListener

      public void removeCameraChangeListener(CameraChangeListener l)
      Unregisters a camera-change listener.
      Specified by:
      removeCameraChangeListener in interface MapSurface
    • 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.
      Specified by:
      isNativeMap in interface MapSurface
    • asComponent

      public Component asComponent()
      This surface as a Codename One Component for layout purposes.
      Specified by:
      asComponent in interface MapSurface
    • paintBackground

      protected void paintBackground(Graphics g)
      Description copied from class: Component

      This method paints the Component background, it should be overriden by subclasses to perform custom background drawing.

      Parameters
      • g: the component graphics
      Overrides:
      paintBackground in class Component
    • pointerPressed

      public void pointerPressed(int x, int y)

      If this Component is focused, the pointer pressed event will call this method

      Parameters
      • x: the pointer x coordinate

      • y: the pointer y coordinate

      Overrides:
      pointerPressed in class Container
    • pointerDragged

      public void pointerDragged(int x, int y)

      If this Component is focused, the pointer dragged event will call this method

      Parameters
      • x: the pointer x coordinate

      • y: the pointer y coordinate

      Overrides:
      pointerDragged in class Component
    • pointerReleased

      public void pointerReleased(int x, int y)

      If this Component is focused, the pointer released event will call this method

      Parameters
      • x: the pointer x coordinate

      • y: the pointer y coordinate

      Overrides:
      pointerReleased in class Component
    • 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
      Overrides:
      longPointerPress in class Component
    • pinch

      protected boolean pinch(float scale)
      Description copied from class: Component

      Invoked by subclasses interested in handling pinch to zoom events, if true is returned other drag events will not be broadcast

      Parameters
      • scale: @param scale 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

      Overrides:
      pinch in class Component
    • calcPreferredSize

      protected Dimension calcPreferredSize()
      Description copied from class: Container

      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

      Overrides:
      calcPreferredSize in class Container