MapLibre Native for Qt
|
The Map class is a Qt wrapper for the MapLibre Native engine. More...
#include <QMapLibre/Map>
Public Slots | |
void | render () |
Render. | |
void | setConnectionEstablished () |
Set connection established. | |
void | startStaticRender () |
Start the static renderer. | |
Signals | |
void | needsRendering () |
Signal emitted when the rendering is needed. | |
void | mapChanged (Map::MapChange) |
Signal emitted when the map has changed. | |
void | mapLoadingFailed (Map::MapLoadingFailure, const QString &reason) |
Signal emitted when a map loading failure happens. | |
void | copyrightsChanged (const QString ©rightsHtml) |
Signal emitted when the copyrights have changed. | |
void | staticRenderFinished (const QString &error) |
Signal emitted when a static map is fully drawn. | |
Public Member Functions | |
Map (QObject *parent=nullptr, const Settings &settings=Settings(), const QSize &size=QSize(), qreal pixelRatio=1) | |
Main constructor for Map. | |
QString | styleJson () const |
Get the map style JSON. | |
QString | styleUrl () const |
Get the map style URL. | |
void | setStyleJson (const QString &) |
Set a new style from a JSON. | |
void | setStyleUrl (const QString &) |
Set the map style URL. | |
double | latitude () const |
Get the map latitude. | |
void | setLatitude (double latitude) |
Set the map latitude. | |
double | longitude () const |
Get the map longitude. | |
void | setLongitude (double longitude) |
Set the map longitude. | |
double | scale () const |
Get the map scale factor. | |
void | setScale (double scale, const QPointF ¢er=QPointF()) |
Set the map scale factor. | |
double | zoom () const |
Get the map zoom factor. | |
void | setZoom (double zoom) |
Set the map zoom factor. | |
double | minimumZoom () const |
Get the minimum zoom level allowed for the map. | |
double | maximumZoom () const |
Get the maximum zoom level allowed for the map. | |
double | bearing () const |
Get the bearing angle. | |
void | setBearing (double degrees) |
Set the bearing angle. | |
void | setBearing (double degrees, const QPointF ¢er) |
Set the bearing angle. | |
double | pitch () const |
Get the pitch angle. | |
void | setPitch (double pitch) |
Set the pitch angle. | |
void | pitchBy (double pitch) |
Pitch the map for an angle. | |
NorthOrientation | northOrientation () const |
Get the map north orientation mode. | |
void | setNorthOrientation (NorthOrientation) |
Set the north orientation mode. | |
Coordinate | coordinate () const |
Get the map center coordinate. | |
void | setCoordinate (const Coordinate &coordinate) |
Set the map center coordinate. | |
void | setCoordinateZoom (const Coordinate &coordinate, double zoom) |
Convenience method for setting the coordinate and zoom simultaneously. | |
void | jumpTo (const CameraOptions &) |
Atomically jump to the camera options. | |
void | setGestureInProgress (bool inProgress) |
Set gesture in progress status. | |
void | setTransitionOptions (qint64 duration, qint64 delay=0) |
Set transition options. | |
void | addAnnotationIcon (const QString &name, const QImage &sprite) |
Add an annotation icon to the map. | |
AnnotationID | addAnnotation (const Annotation &annotation) |
Add an annotation to the map. | |
void | updateAnnotation (AnnotationID id, const Annotation &annotation) |
Update an existing annotation. | |
void | removeAnnotation (AnnotationID id) |
Remove an existing annotation. | |
bool | setLayoutProperty (const QString &layerId, const QString &propertyName, const QVariant &value) |
Set layer layout property. | |
bool | setPaintProperty (const QString &layerId, const QString &propertyName, const QVariant &value) |
Set layer paint property. | |
bool | isFullyLoaded () const |
Get loaded state. | |
void | moveBy (const QPointF &offset) |
Pan the map by an offset. | |
void | scaleBy (double scale, const QPointF ¢er=QPointF()) |
Scale the map. | |
void | rotateBy (const QPointF &first, const QPointF &second) |
Rotate the map. | |
void | resize (const QSize &size) |
Resize the map. | |
QPointF | pixelForCoordinate (const Coordinate &coordinate) const |
Get pixel for coordinate. | |
Coordinate | coordinateForPixel (const QPointF &pixel) const |
Get coordinate for pixel. | |
CoordinateZoom | coordinateZoomForBounds (const Coordinate &sw, const Coordinate &ne) const |
Get the coordinate and zoom for required bounds. | |
CoordinateZoom | coordinateZoomForBounds (const Coordinate &sw, const Coordinate &ne, double bearing, double pitch) |
Get the coordinate and zoom for required bounds with updated bearing and pitch. | |
void | setMargins (const QMargins &margins) |
Set the map margins. | |
QMargins | margins () const |
Get the map margins. | |
void | addSource (const QString &id, const QVariantMap ¶ms) |
Add a style source. | |
bool | sourceExists (const QString &id) |
Check if a style source exists. | |
void | updateSource (const QString &id, const QVariantMap ¶ms) |
Update a style source. | |
void | removeSource (const QString &id) |
Remove a style source. | |
void | addImage (const QString &id, const QImage &sprite) |
Add a style image. | |
void | removeImage (const QString &id) |
Remove a style image. | |
void | addCustomLayer (const QString &id, std::unique_ptr< CustomLayerHostInterface > host, const QString &before=QString()) |
Add a custom style layer. | |
void | addLayer (const QString &id, const QVariantMap ¶ms, const QString &before=QString()) |
Add a style layer. | |
bool | layerExists (const QString &id) |
Check if a style layer exists. | |
void | removeLayer (const QString &id) |
Remove a style layer. | |
QVector< QString > | layerIds () const |
List all existing layers. | |
void | setFilter (const QString &layerId, const QVariant &filter) |
Set a style filter to a layer. | |
QVariant | getFilter (const QString &layerId) const |
Get a style filter of a layer. | |
void | createRenderer () |
Create the renderer. | |
void | destroyRenderer () |
Destroy the renderer. | |
void | setOpenGLFramebufferObject (quint32 fbo, const QSize &size) |
Set the OpenGL framebuffer object. | |
Properties | |
double | latitude |
The map's current latitude in degrees. | |
double | longitude |
The map current longitude in degrees. | |
double | zoom |
the map zoom factor. | |
double | bearing |
the map bearing in degrees. | |
double | pitch |
the map pitch in degrees. | |
QString | styleJson |
The map's current style JSON. | |
QString | styleUrl |
The map's current style URL. | |
double | scale |
The map scale factor. | |
QMapLibre::Coordinate | coordinate |
The map center coordinate. | |
QMargins | margins |
the map margins in pixels from the corners of the map. | |
The Map class is a Qt wrapper for the MapLibre Native engine.
Map is a Qt friendly version the MapLibre Native engine using Qt types and deep integration with Qt event loop. Map relies as much as possible on Qt, trying to minimize the external dependencies. For instance it will use QNetworkAccessManager
for HTTP requests and QString
for UTF-8 manipulation.
Map is not thread-safe and it is assumed that it will be accessed from the same thread as the thread where the OpenGL context lives.
This enum represents the last changed occurred to the map state.
Enumerator | |
---|---|
MapChangeRegionWillChange | A region of the map will change, like when resizing the map. |
MapChangeRegionWillChangeAnimated | Not in use by Map. |
MapChangeRegionIsChanging | A region of the map is changing. |
MapChangeRegionDidChange | A region of the map finished changing. |
MapChangeRegionDidChangeAnimated | Not in use by Map. |
MapChangeWillStartLoadingMap | The map is getting loaded. This state is set only once right after Map is created and a style is set. |
MapChangeDidFinishLoadingMap | All the resources were loaded and parsed and the map is fully rendered. After this state the mapChanged() signal won't fire again unless the is some client side interaction with the map or a tile expires, causing a new resource to be requested from the network. |
MapChangeDidFailLoadingMap | An error occurred when loading the map. |
MapChangeWillStartRenderingFrame | Just before rendering the frame. This is the state of the map just after calling render() and might happened many times before the map is fully loaded. |
MapChangeDidFinishRenderingFrame | The current frame was rendered but was left in a partial state. Some parts of the map might be missing because have not arrived from the network or are being parsed. |
MapChangeDidFinishRenderingFrameFullyRendered | The current frame was fully rendered. |
MapChangeWillStartRenderingMap | Set once when the map is about to get rendered for the first time. |
MapChangeDidFinishRenderingMap | Not in use by Map. |
MapChangeDidFinishRenderingMapFullyRendered | Map is fully loaded and rendered. |
MapChangeDidFinishLoadingStyle | The style was loaded. |
MapChangeSourceDidChange | A source has changed. |
This enum represents map loading failure type.
This enum sets the orientation of the north bearing. It will directly affect bearing when resetting the north (i.e. setting bearing to 0).
|
explicit |
Main constructor for Map.
parent | The parent object. |
settings | The settings for the map. |
size | The size of the viewport. |
pixelRatio | The initial pixel density of the screen. |
Constructs a Map object with settings and sets the parent object. The settings cannot be changed after the object is constructed.
AnnotationID QMapLibre::Map::addAnnotation | ( | const Annotation & | annotation | ) |
Add an annotation to the map.
annotation | The annotation to add. |
void QMapLibre::Map::addAnnotationIcon | ( | const QString & | name, |
const QImage & | sprite ) |
Add an annotation icon to the map.
name | The icon name. |
sprite | The icon image. |
Adds an sprite to the annotation icon pool. This can be later used by the annotation functions to shown any drawing on the map by referencing its name.
Unlike using addIcon() for runtime styling, annotations added with addAnnotation() will survive style changes.
void QMapLibre::Map::addCustomLayer | ( | const QString & | id, |
std::unique_ptr< CustomLayerHostInterface > | host, | ||
const QString & | before = QString() ) |
Add a custom style layer.
id | The layer identifier. |
host | The custom layer host interface. |
before | The layer identifier before which the new layer will be added. |
Adds a custom layer with id with the custom layer host interface host before the existing layer before.
void QMapLibre::Map::addImage | ( | const QString & | id, |
const QImage & | sprite ) |
Add a style image.
id | The image identifier. |
sprite | The image. |
Adds the sprite with the identifier id that can be used later by a symbol layer.
If the id was already added, it gets replaced by the new image only if the dimensions of the image are the same as the old image, otherwise it has no effect.
void QMapLibre::Map::addLayer | ( | const QString & | id, |
const QVariantMap & | params, | ||
const QString & | before = QString() ) |
Add a style layer.
id | The layer identifier. |
params | The layer parameters. |
before | The layer identifier before which the new layer will be added. |
Adds a style layer to the map as specified by the MapLibre style specification with parameters. The layer will be added under the layer specified by before, if specified. Otherwise it will be added as the topmost layer.
This example shows how to add a layer that will be used to show a route line on the map. Note that nothing will be drawn until we set paint properties using setPaintProperty().
void QMapLibre::Map::addSource | ( | const QString & | id, |
const QVariantMap & | params ) |
Add a style source.
id | The source identifier. |
params | The source parameters. |
Adds a source with id to the map as specified by the MapLibre Style Specification with parameters.
This example reads a GeoJSON from the Qt resource system and adds it as source:
|
nodiscard |
|
nodiscard |
|
nodiscard |
Get coordinate for pixel.
pixel | The pixel coordinate. |
Returns the geographic coordinate for the pixel coordinate.
|
nodiscard |
Get the coordinate and zoom for required bounds.
sw | The southwest coordinate. |
ne | The northeast coordinate. |
Returns the coordinate and zoom combination needed in order to make the coordinate bounding box sw and ne visible.
|
nodiscard |
Get the coordinate and zoom for required bounds with updated bearing and pitch.
sw | The southwest coordinate. |
ne | The northeast coordinate. |
newBearing | The updated bearing. |
newPitch | The updated pitch. |
Returns the coordinate and zoom combination needed in order to make the coordinate bounding box sw and ne visible taking into account newBearing and newPitch.
|
signal |
Signal emitted when the copyrights have changed.
copyrightsHtml | The HTML of the copyrights. |
This signal is emitted when the copyrights of the current content of the map have changed. This can be caused by a style change or adding a new source.
void QMapLibre::Map::createRenderer | ( | ) |
Create the renderer.
Creates the infrastructure needed for rendering the map. It should be called before any call to render().
Must be called on the render thread.
void QMapLibre::Map::destroyRenderer | ( | ) |
Destroy the renderer.
Destroys the infrastructure needed for rendering the map, releasing resources.
Must be called on the render thread.
|
nodiscard |
Get a style filter of a layer.
layerId | The layer identifier. |
Returns the current expression-based filter value applied to a layer with ID layerId, if any.
Filter value types are described in the MapLibre Style Spec.
|
nodiscard |
Get loaded state.
true
if the map is completely rendered, false
otherwise.A partially rendered map ranges from nothing rendered at all to only labels missing.
void QMapLibre::Map::jumpTo | ( | const CameraOptions & | camera | ) |
Atomically jump to the camera options.
camera | The camera options. |
|
nodiscard |
Get the map latitude.
bool QMapLibre::Map::layerExists | ( | const QString & | id | ) |
Check if a style layer exists.
id | The layer identifier. |
true
if the layer with given id exists, false
otherwise.
|
nodiscard |
List all existing layers.
|
nodiscard |
Get the map longitude.
|
signal |
Signal emitted when the map has changed.
change | The map change mode. |
This signal is emitted when the state of the map has changed. This signal may be used for detecting errors when loading a style or detecting when a map is fully loaded by analyzing the parameter change.
|
signal |
Signal emitted when a map loading failure happens.
type | The type of failure. |
description | The description of the failure. |
This signal is emitted when a map loading failure happens. Details of the failures are provided, including its type and textual description.
|
nodiscard |
Get the map margins.
|
nodiscard |
Get the maximum zoom level allowed for the map.
|
nodiscard |
Get the minimum zoom level allowed for the map.
void QMapLibre::Map::moveBy | ( | const QPointF & | offset | ) |
Pan the map by an offset.
offset | The offset in pixels. |
The pixel coordinate origin is located at the upper left corner of the map.
|
signal |
Signal emitted when the rendering is needed.
This signal is emitted when the visual contents of the map have changed and a redraw is needed in order to keep the map visually consistent with the current state.
|
nodiscard |
Get the map north orientation mode.
|
nodiscard |
void QMapLibre::Map::pitchBy | ( | double | pitch | ) |
|
nodiscard |
Get pixel for coordinate.
coordinate | The geographic coordinate. |
Returns the offset in pixels for coordinate. The origin pixel coordinate is located at the top left corner of the map view.
This method returns the correct value for any coordinate, even if the coordinate is not currently visible on the screen.
void QMapLibre::Map::removeAnnotation | ( | AnnotationID | id | ) |
Remove an existing annotation.
id | The unique identifier for the annotation to remove. |
void QMapLibre::Map::removeImage | ( | const QString & | id | ) |
Remove a style image.
id | The image identifier. |
void QMapLibre::Map::removeLayer | ( | const QString & | id | ) |
Remove a style layer.
id | The layer identifier. |
This method has no effect if the style layer does not exist.
void QMapLibre::Map::removeSource | ( | const QString & | id | ) |
Remove a style source.
id | The source identifier. |
This method has no effect if the style source does not exist.
|
slot |
Render.
Renders the map using OpenGL draw calls. It will make sure to bind the framebuffer object before drawing; otherwise a valid OpenGL context is expected with an appropriate OpenGL viewport state set for the size of the canvas.
This function should be called only after the signal needsRendering() is emitted at least once.
Must be called on the render thread.
void QMapLibre::Map::resize | ( | const QSize & | size | ) |
Resize the map.
size | The new size. |
Resize the map to size and scale to fit at the framebuffer. For high DPI screens, the size will be smaller than the framebuffer.
void QMapLibre::Map::rotateBy | ( | const QPointF & | first, |
const QPointF & | second ) |
Rotate the map.
first | The first screen coordinate. |
second | The second screen coordinate. |
Rotate the map from the first screen coordinate to the second screen coordinate. This method can be used for implementing rotating the map by clicking and dragging, being first the cursor coordinate at the last frame and second the cursor coordinate at the current frame.
|
nodiscard |
void QMapLibre::Map::scaleBy | ( | double | scale, |
const QPointF & | center = QPointF() ) |
Scale the map.
scale | The scale factor. |
center | The direction pixel coordinate. |
Scale the map by scale in the direction of the center. This function can be used for implementing a pinch gesture.
void QMapLibre::Map::setBearing | ( | double | degrees | ) |
Set the bearing angle.
degrees | The bearing angle in degrees. |
Negative values and values over 360 are valid and will wrap. The direction of the rotation is counterclockwise.
void QMapLibre::Map::setBearing | ( | double | degrees, |
const QPointF & | center ) |
Set the bearing angle.
degrees | The bearing angle in degrees. |
center | The center pixel coordinate. |
Negative values and values over 360 are valid and will wrap. The direction of the rotation is counterclockwise.
When center is defined, the map will rotate around the center pixel coordinate respecting the margins if defined.
|
slot |
Set connection established.
Informs the map that the network connection has been established, causing all network requests that previously timed out to be retried immediately.
void QMapLibre::Map::setCoordinate | ( | const Coordinate & | coordinate | ) |
Set the map center coordinate.
coordinate | The map center coordinate. |
Centers the map at a geographic coordinate respecting the margins, if set.
void QMapLibre::Map::setCoordinateZoom | ( | const Coordinate & | coordinate, |
double | zoom ) |
Convenience method for setting the coordinate and zoom simultaneously.
coordinate | The map center coordinate. |
zoom | The map zoom factor. |
void QMapLibre::Map::setFilter | ( | const QString & | layerId, |
const QVariant & | filter ) |
Set a style filter to a layer.
layerId | The layer identifier. |
filter | The filter expression. |
Adds a filter to a style layer using the format described in the MapLibre Style Spec.
Given a layer marker
from an arbitrary GeoJSON source containing features of type "Point" and "LineString", this example shows how to make sure the layer will only tag features of type "Point".
void QMapLibre::Map::setGestureInProgress | ( | bool | progress | ) |
Set gesture in progress status.
progress | The gesture in progress status. |
Tells the map rendering engine that there is currently a gesture in progress. This affects how the map renders labels, as it will use different textur filters if a gesture is ongoing.
void QMapLibre::Map::setLatitude | ( | double | latitude | ) |
Set the map latitude.
latitude | The map latitude in degrees. |
Setting a latitude doesn't necessarily mean it will be accepted since Map might constrain it within the limits of the Web Mercator projection.
bool QMapLibre::Map::setLayoutProperty | ( | const QString & | layerId, |
const QString & | propertyName, | ||
const QVariant & | value ) |
Set layer layout property.
layerId | The layer identifier. |
propertyName | The layout property name. |
value | The layout property value. |
true
if the operation succeeds, false
otherwise.Sets a layout property value to an existing layer. The property string can be any as defined by the MapLibre Style Spec for layout properties.
The implementation attempts to treat value as a JSON string, if the QVariant
inner type is a string. If not a valid JSON string, then it'll proceed with the mapping described below.
This example hides the layer route:
This table describes the mapping between style types and Qt types accepted by setLayoutProperty():
MapLibre style type | Qt type |
---|---|
Enum | QString |
String | QString |
Boolean | bool |
Number | int , double or float |
Array | QVariantList |
void QMapLibre::Map::setLongitude | ( | double | longitude | ) |
Set the map longitude.
longitude | The map longitude in degrees. |
Setting a longitude beyond the limits of the Web Mercator projection will make the map wrap. As an example, setting the longitude to 360 is effectively the same as setting it to 0.
void QMapLibre::Map::setMargins | ( | const QMargins & | margins | ) |
Set the map margins.
margins | The map margins in pixels. |
This sets a new reference center for the map.
void QMapLibre::Map::setNorthOrientation | ( | NorthOrientation | orientation | ) |
Set the north orientation mode.
orientation | The north orientation mode. |
void QMapLibre::Map::setOpenGLFramebufferObject | ( | quint32 | fbo, |
const QSize & | size ) |
Set the OpenGL framebuffer object.
fbo | The OpenGL framebuffer object ID. |
size | The OpenGL framebuffer size. |
If MapLibre Native needs to rebind the default fbo, it will use the ID supplied here. size is the size of the framebuffer, which on high DPI screens is usually bigger than the map size.
Must be called on the render thread.
bool QMapLibre::Map::setPaintProperty | ( | const QString & | layerId, |
const QString & | propertyName, | ||
const QVariant & | value ) |
Set layer paint property.
layerId | The layer identifier. |
propertyName | The paint property name. |
value | The paint property value. |
true
if the operation succeeds, false
otherwise.Sets a layout property value to an existing layer. The property string can be any as defined by the MapLibre Style Spec for paint properties.
The implementation attempts to treat value as a JSON string, if the QVariant
inner type is a string. If not a valid JSON string, then it'll proceed with the mapping described below.
For paint properties that take a color as value, such as fill-color, a string such as blue
can be passed or a QColor
.
This table describes the mapping between style types and Qt types accepted by setPaintProperty():
MapLibre style type | Qt type |
---|---|
Color | QString or QColor |
Enum | QString |
String | QString |
Boolean | bool |
Number | int , double or float |
Array | QVariantList |
If the style specification defines the property's type as Array, use a QVariantList
. For example, the following code sets a route
layer's line-dasharray
property:
void QMapLibre::Map::setPitch | ( | double | pitch | ) |
void QMapLibre::Map::setScale | ( | double | scale, |
const QPointF & | center = QPointF() ) |
Set the map scale factor.
scale | The map scale factor. |
center | The center pixel coordinate. |
This property is used to zoom the map. When center is defined, the map will scale in the direction of the center pixel coordinates. The center will remain at the same pixel coordinate after scaling as before calling this method.
void QMapLibre::Map::setStyleJson | ( | const QString & | style | ) |
Set a new style from a JSON.
style | The style JSON. |
The JSON must conform to the MapLibre Style spec.
void QMapLibre::Map::setStyleUrl | ( | const QString & | url | ) |
Set the map style URL.
url | The map style URL. |
Sets a URL for fetching a JSON that will be later fed to setStyleJson. URLs using the Mapbox scheme (mapbox://
) are also accepted and translated automatically to an actual HTTPS request.
The Mapbox scheme is not enforced and a style can be fetched from anything that QNetworkAccessManager
can handle.
void QMapLibre::Map::setTransitionOptions | ( | qint64 | duration, |
qint64 | delay = 0 ) |
Set transition options.
duration | The transition duration in milliseconds. |
delay | The transition delay in milliseconds. |
Style paint property values transition to new values with animation when they are updated.
void QMapLibre::Map::setZoom | ( | double | zoom | ) |
Set the map zoom factor.
zoom | The map zoom factor. |
This property is used to zoom the map. When center is defined, the map will zoom in the direction of the center. This function could be used for implementing a pinch gesture or zooming by using the mouse scroll wheel.
bool QMapLibre::Map::sourceExists | ( | const QString & | id | ) |
Check if a style source exists.
id | The source identifier. |
true
if the layer with given id exists, false
otherwise.
|
slot |
Start the static renderer.
Start a static rendering of the current state of the map. This should only be called when the map is initialized in static mode.
|
signal |
Signal emitted when a static map is fully drawn.
error | The error message. |
This signal is emitted when a static map is fully drawn. Usually the next step is to extract the map from a framebuffer into a container like a QImage
. error is set to a message when an error occurs.
|
nodiscard |
Get the map style JSON.
|
nodiscard |
Get the map style URL.
void QMapLibre::Map::updateAnnotation | ( | AnnotationID | id, |
const Annotation & | annotation ) |
Update an existing annotation.
id | The unique identifier for the annotation to update. |
annotation | The updated annotation. |
void QMapLibre::Map::updateSource | ( | const QString & | id, |
const QVariantMap & | params ) |
Update a style source.
id | The source identifier. |
params | The source parameters. |
If the source does not exist, it will be added like in addSource(). Only image and GeoJSON sources can be updated.
|
nodiscard |
|
readwrite |
the map bearing in degrees.
|
readwrite |
The map center coordinate.
|
readwrite |
The map's current latitude in degrees.
|
readwrite |
The map current longitude in degrees.
|
readwrite |
the map margins in pixels from the corners of the map.
|
readwrite |
the map pitch in degrees.
|
readwrite |
The map scale factor.
|
readwrite |
The map's current style JSON.
|
readwrite |
The map's current style URL.