Class NativeMap

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

public class NativeMap extends Container implements MapSurface

A native-rendered map. When the build wired in a native provider (Apple MapKit, Google Maps, Bing, Huawei, ... selected via the maps.provider build hint) and it is available on the device, NativeMap embeds that provider's native view as a PeerComponent. Otherwise -- on the simulator, on devices without the selected provider, or when no provider was wired in at all -- it transparently falls back to an embedded pure-vector MapView. Either way it exposes the same MapSurface API, so application code is identical.

The public API never names a provider; which one (if any) backs a given build is decided entirely by build hints through MapProviderRegistry.

  • Constructor Details

    • NativeMap

      public NativeMap()
      Creates a native map centered on the equator at a low zoom.
    • NativeMap

      public NativeMap(LatLng center, double zoom)
      Creates a native map at the given initial camera.
    • NativeMap

      public NativeMap(LatLng center, double zoom, TileSource fallbackSource, MapStyle fallbackStyle)
      Creates a native map at the given initial camera, specifying the tile source and style used by the pure-vector MapView when no native provider is available. Useful for an offline or branded fallback basemap (and for deterministic tests).
  • Method Details

    • initComponent

      protected void initComponent()
      Allows subclasses to bind functionality that relies on fully initialized and "ready for action" component state
      Overrides:
      initComponent in class Component
    • 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
    • fireTap

      public static void fireTap(int mapId, int x, int y)
      Invoked from native code when the map is tapped.
    • fireLongPress

      public static void fireLongPress(int mapId, int x, int y)
      Invoked from native code when the map is long-pressed.
    • fireMarkerClick

      public static void fireMarkerClick(int mapId, long markerKey)
      Invoked from native code when a marker is tapped (markerKey is the value returned by MapProvider.addMarker(int, byte[], double, double, String, String, float, float)).
    • fireCameraChange

      public static void fireCameraChange(int mapId)
      Invoked from native code when the camera settles after movement.