Map QML Type

The Map type displays a map. 更多...

import 语句: import QtLocation 5.15
Since: QtLocation 5.0

特性

信号

方法

详细描述

The Map type is used to display a map or image of the Earth, with the capability to also display interactive objects tied to the map's surface.

There are a variety of different ways to visualize the Earth's surface in a 2-dimensional manner, but all of them involve some kind of projection: a mathematical relationship between the 3D coordinates (latitude, longitude and altitude) and 2D coordinates (X and Y in pixels) on the screen.

Different sources of map data can use different projections, and from the point of view of the Map type, we treat these as one replaceable unit: the Map plugin. A Map plugin consists of a data source, as well as all other details needed to display its data on-screen.

The current Map plugin in use is contained in the plugin property of the Map item. In order to display any image in a Map item, you will need to set this property. See the Plugin type for a description of how to retrieve an appropriate plugin for use.

The geographic region displayed in the Map item is referred to as its viewport, and this is defined by the properties center ,和 zoomLevel center property contains a coordinate specifying the center of the viewport, while zoomLevel controls the scale of the map. See each of these properties for further details about their values.

When the map is displayed, each possible geographic coordinate that is visible will map to some pixel X and Y coordinate on the screen. To perform conversions between these two, Map provides the toCoordinate and fromCoordinate functions, which are of general utility.

Map Objects

Map related objects can be declared within the body of a Map object in Qt Quick and will automatically appear on the Map. To add an object programmatically, first be sure it is created with the Map as its parent (for example in an argument to Component::createObject ). Then call the addMapItem method on the Map, if the type of this object is one of MapCircle , MapRectangle , MapPolyline , MapPolygon , MapRoute or MapQuickItem . A corresponding removeMapItem method also exists to do the opposite and remove any of the above types of map objects from the Map.

Moving Map objects around, resizing them or changing their shape normally does not involve any special interaction with Map itself -- changing these properties in a map object will automatically update the display.

交互

The Map type includes support for pinch and flick gestures to control zooming and panning. These are enabled by default, and available at any time by using the gesture object. The actual GestureArea is constructed specially at startup and cannot be replaced or destroyed. Its properties can be altered, however, to control its behavior.

性能

Maps are rendered using OpenGL (ES) and the Qt Scene Graph stack, and as a result perform quite well where GL accelerated hardware is available.

For "online" Map plugins, network bandwidth and latency can be major contributors to the user's perception of performance. Extensive caching is performed to mitigate this, but such mitigation is not always perfect. For "offline" plugins, the time spent retrieving the stored geographic data and rendering the basic map features can often play a dominant role. Some offline plugins may use hardware acceleration themselves to (partially) avert this.

In general, large and complex Map items such as polygons and polylines with large numbers of vertices can have an adverse effect on UI performance. Further, more detailed notes on this are in the documentation for each map item type.

用法范例

The following snippet shows a simple Map and the necessary Plugin type to use it. The map is centered over Oslo, Norway, with zoom level 14.

import QtQuick 2.0
import QtQuick.Window 2.14
import QtLocation 5.6
import QtPositioning 5.6
Window {
    width: Qt.platform.os == "android" ? Screen.width : 512
    height: Qt.platform.os == "android" ? Screen.height : 512
    visible: true
    Plugin {
        id: mapPlugin
        name: "osm" // "mapboxgl", "esri", ...
        // specify plugin parameters if necessary
        // PluginParameter {
        //     name:
        //     value:
        // }
    }
    Map {
        anchors.fill: parent
        plugin: mapPlugin
        center: QtPositioning.coordinate(59.91, 10.75) // Oslo
        zoomLevel: 14
    }
}
					

特性文档编制

activeMapType : MapType

Access to the currently active map type .

This property can be set to change the active map type 。见 supportedMapTypes property for possible values.

另请参阅 MapType .

bearing : real

This property holds the bearing for the map. The default value is 0. If the Plugin used for the Map supports bearing, the valid range for this value is between 0 and 360. If the Plugin used for the Map does not support bearing, changing this property will have no effect.

This property was introduced in QtLocation 5.9.

center : coordinate

This property holds the coordinate which occupies the center of the mapping viewport. Invalid center coordinates are ignored.

The default value is an arbitrary valid coordinate.

color : color

This property holds the background color of the map element.

该特性在 Qt 5.6 引入。

copyrightsVisible : bool

This property holds the visibility of the copyrights notice. The notice is usually displayed in the bottom left corner. By default, this property is set to true .

注意: Many map providers require the notice to be visible as part of the terms and conditions. Please consult the relevant provider documentation before turning this notice off.

该特性在 Qt 5.7 引入。

error : enumeration

This read-only property holds the last occurred mapping service provider error.

  • Map. NoError - No error has occurred.
  • Map.NotSupportedError -The maps plugin property was not set or there is no mapping manager associated with the plugin.
  • Map.UnknownParameterError -The plugin did not recognize one of the parameters it was given.
  • Map.MissingRequiredParameterError - The plugin did not find one of the parameters it was expecting.
  • Map.ConnectionError - The plugin could not connect to its backend service or database.

另请参阅 QGeoServiceProvider::Error .

errorString : string

This read-only property holds the textual presentation of the latest mapping provider error. If no error has occurred, an empty string is returned.

An empty string may also be returned if an error occurred which has no associated textual representation.

另请参阅 QGeoServiceProvider::errorString() .

fieldOfView : real

This property holds the field of view of the camera used to look at the map, in degrees. If the plugin property of the map is not set, or the plugin does not support mapping, the value is 45 degrees.

Note that changing this value implicitly changes also the distance between the camera and the map, so that, at a tilting angle of 0 degrees, the resulting image is identical for any value used for this property.

For more information about this parameter, consult the Wikipedia articles about Field of view and Angle of view .

This property was introduced in QtLocation 5.9.

另请参阅 minimumFieldOfView and maximumFieldOfView .

gesture : MapGestureArea

Contains the MapGestureArea created with the Map. This covers pan, flick and pinch gestures. Use gesture.enabled: true to enable basic gestures, or see MapGestureArea 进一步了解细节。

mapItems : list < MapItem >

Returns the list of all map items in no particular order. These items include items that were declared statically as part of the type declaration, as well as dynamical items ( addMapItem , MapItemView ).

另请参阅 addMapItem , removeMapItem ,和 clearMapItems .

mapParameters : list < MapParameters >

Returns the list of all map parameters in no particular order. These items include map parameters that were declared statically as part of the type declaration, as well as dynamical map parameters ( addMapParameter ).

The release of this API with Qt 5.9 is a Technology Preview.

该特性在 Qt 5.9 引入。

另请参阅 MapParameter , addMapParameter , removeMapParameter ,和 clearMapParameters .

mapReady : bool

This property holds whether the map has been successfully initialized and is ready to be used. Some methods, such as fromCoordinate and toCoordinate , will not work before the map is ready. Due to the architecture of the Map , it's advised to use the signal emitted for this property in place of Component.onCompleted , to make sure that everything behaves as expected.

该特性在 Qt 5.9 引入。

maximumFieldOfView : real

This property holds the maximum valid field of view for the map, in degrees.

The minimum tilt field of view by the plugin used is an upper bound for this property. If the plugin property is not set or the plugin does not support mapping, this property is 179 .

This property was introduced in QtLocation 5.9.

另请参阅 fieldOfView and minimumFieldOfView .

maximumTilt : real

This property holds the maximum valid tilt for the map, in degrees.

The maximum tilt defined by the plugin used is an upper bound for this property. If the plugin property is not set or the plugin does not support mapping, this property is 89.5 .

由于 QtLocation 5.12, plugins can additionally restrict this value depending on the current zoom level.

This property was introduced in QtLocation 5.9.

另请参阅 tilt and minimumTilt .

maximumZoomLevel : real

This property holds the maximum valid zoom level for the map.

The maximum zoom level is defined by the plugin used. If the plugin property is not set or the plugin does not support mapping, this property is 30 .

minimumFieldOfView : real

This property holds the minimum valid field of view for the map, in degrees.

The minimum tilt field of view by the plugin used is a lower bound for this property. If the plugin property is not set or the plugin does not support mapping, this property is 1 .

This property was introduced in QtLocation 5.9.

另请参阅 fieldOfView and maximumFieldOfView .

minimumTilt : real

This property holds the minimum valid tilt for the map, in degrees.

The minimum tilt defined by the plugin used is a lower bound for this property. If the plugin property is not set or the plugin does not support mapping, this property is 0 .

由于 QtLocation 5.12, plugins can additionally restrict this value depending on the current zoom level.

This property was introduced in QtLocation 5.9.

另请参阅 tilt and maximumTilt .

minimumZoomLevel : real

This property holds the minimum valid zoom level for the map.

The minimum zoom level defined by the plugin used is a lower bound for this property. However, the returned value is also canvas-size-dependent, and can be higher than the user-specified value, or than the minimum zoom level defined by the plugin used, to prevent the map from being smaller than the viewport in either dimension.

plugin property is not set or the plugin does not support mapping, this property is 0 .

plugin : Plugin

This property holds the plugin which provides the mapping functionality.

This is a write-once property. Once the map has a plugin associated with it, any attempted modifications of the plugin will be ignored.

supportedMapTypes : list < MapType >

This read-only property holds the set of map types supported by this map.

另请参阅 activeMapType .

tilt : real

This property holds the tilt for the map, in degrees. The default value is 0. The valid range for this value is [ minimumTilt , maximumTilt ]. If the Plugin used for the Map does not support tilting, changing this property will have no effect.

This property was introduced in QtLocation 5.9.

另请参阅 minimumTilt and maximumTilt .

visibleArea : rect

This property holds the visible area inside the Map QML element. It is a rect whose coordinates are relative to the Map element. Its size will be clamped to the size of the Map element. A null visibleArea means that the whole Map is visible.

该特性在 Qt 5.12 引入。

visibleRegion : geoshape

This property holds the region which occupies the viewport of the map. The camera is positioned in the center of the shape, and at the largest integral zoom level possible which allows the whole shape to be visible on the screen. This implies that reading this property back shortly after having been set the returned area is equal or larger than the set area.

Setting this property implicitly changes the center and zoomLevel of the map. Any previously set value to those properties will be overridden.

注意: Since Qt 5.14 This property provides change notifications.

该特性在 Qt 5.6 引入。

zoomLevel : real

This property holds the zoom level for the map.

Larger values for the zoom level provide more detail. Zoom levels are always non-negative. The default value is 8.0. Depending on the plugin in use, values outside the [ minimumZoomLevel , maximumZoomLevel ] range, which represent the range for which tiles are available, may be accepted, or clamped.

信号文档编制

copyrightLinkActivated ( string link )

This signal is emitted when the user clicks on a link in the copyright notice. The application should open the link in a browser or display its contents to the user.

注意: 相应处理程序是 onCopyrightLinkActivated .

方法文档编制

void addMapItem ( MapItem item )

添加给定 item to the Map (for example MapQuickItem , MapCircle ). If the object already is on the Map, it will not be added again.

As an example, consider the case where you have a MapCircle representing your current position:

import QtQuick 2.0
import QtPositioning 5.5
import QtLocation 5.6
PositionSource {
    id: positionSource
}
Map {
    id: map
    property MapCircle circle
    Component.onCompleted: {
        circle = Qt.createQmlObject('import QtLocation 5.3; MapCircle {}', page)
        circle.center = positionSource.position.coordinate
        circle.radius = 5000.0
        circle.color = 'green'
        circle.border.width = 3
        map.addMapItem(circle)
    }
}
					

注意: MapItemViews cannot be added with this method.

另请参阅 mapItems , removeMapItem ,和 clearMapItems .

void addMapItemGroup ( MapItemGroup itemGroup )

Adds the map items contained in the given itemGroup to the Map (for example MapQuickItem , MapCircle ).

This method was introduced in Qt 5.9.

另请参阅 MapItemGroup and removeMapItemGroup .

void addMapItemView ( MapItemView itemView )

添加 itemView to the Map.

This method was introduced in Qt 5.10.

另请参阅 MapItemView and removeMapItemView .

void addMapParameter ( MapParameter 参数 )

添加 参数 object to the map. The effect of this call is dependent on the combination of the content of the MapParameter and the type of underlying QGeoMap. If a MapParameter that is not supported by the underlying QGeoMap gets added, the call has no effect.

The release of this API with Qt 5.9 is a Technology Preview.

This method was introduced in Qt 5.9.

另请参阅 MapParameter , removeMapParameter , mapParameters ,和 clearMapParameters .

void alignCoordinateToPoint ( coordinate coordinate , QPointF point )

Aligns coordinate to point . This method effectively extends the functionality offered by the center qml property, allowing to align a coordinate to point of the Map element other than its center. This is useful in those applications where the center of the scene (e.g., a cursor) is not to be placed exactly in the center of the map.

If the map is tilted, and coordinate happens to be behind the camera, or if the map is not ready (see mapReady ), calling this method will have no effect.

The release of this API with Qt 5.10 is a Technology Preview.

This method was introduced in Qt 5.10.

另请参阅 center .

void clearData ()

Clears map data collected by the currently selected plugin.

注意: This method will delete cached files.

另请参阅 plugin .

void clearMapItems ()

Removes all items and item groups from the map.

另请参阅 mapItems , addMapItem , removeMapItem , addMapItemGroup ,和 removeMapItemGroup .

void clearMapParameters ()

Removes all map parameters from the map.

The release of this API with Qt 5.9 is a Technology Preview.

This method was introduced in Qt 5.9.

另请参阅 MapParameter , mapParameters , addMapParameter , removeMapParameter , and clearMapParameters.

void fitViewportToGeoShape ( geoShape , margins )

Fits the viewport to a specific geo shape geoShape margins are in screen pixels.

注意: If the projection used by the plugin is not WebMercator, and the plugin does not have fitting to shape capability, this method will do nothing.

This method was introduced in Qt 5.13.

另请参阅 visibleRegion .

void fitViewportToMapItems ( list < MapItems > = {})

If no argument is provided, fits the current viewport to the boundary of all map items. The camera is positioned in the center of the map items, and at the largest integral zoom level possible which allows all map items to be visible on screen. If is provided, fits the current viewport to the boundary of the specified map items only.

注意: This method gained the optional argument since Qt 5.15. In previous releases, this method fitted the map to all map items.

另请参阅 fitViewportToVisibleMapItems .

void fitViewportToVisibleMapItems ()

Fits the current viewport to the boundary of all visible map items. The camera is positioned in the center of the map items, and at the largest integral zoom level possible which allows all map items to be visible on screen.

另请参阅 fitViewportToMapItems .

point fromCoordinate ( coordinate coordinate , bool clipToViewPort )

Returns the position relative to the map item which corresponds to the coordinate .

clipToViewPort is true , or not supplied then returns an invalid QPointF if coordinate is not within the current viewport.

void pan ( int dx , int dy )

Starts panning the map by dx pixels along the x-axis and by dy pixels along the y-axis.

Positive values for dx move the map right, negative values left. Positive values for dy move the map down, negative values up.

During panning the center ,和 zoomLevel may change.

void prefetchData ()

Optional hint that allows the map to prefetch during this idle period

void removeMapItem ( MapItem item )

移除给定 item from the Map (for example MapQuickItem , MapCircle ). If the MapItem does not exist or was not previously added to the map, the method does nothing.

另请参阅 mapItems , addMapItem ,和 clearMapItems .

void removeMapItemGroup ( MapItemGroup itemGroup )

移除 itemGroup and the items contained therein from the Map.

This method was introduced in Qt 5.9.

另请参阅 MapItemGroup and addMapItemGroup .

void removeMapItemView ( MapItemView itemView )

移除 itemView and the items instantiated by it from the Map.

This method was introduced in Qt 5.10.

另请参阅 MapItemView and addMapItemView .

void removeMapParameter ( MapParameter 参数 )

移除给定 参数 object from the map.

The release of this API with Qt 5.9 is a Technology Preview.

This method was introduced in Qt 5.9.

另请参阅 MapParameter , addMapParameter , mapParameters ,和 clearMapParameters .

void setBearing ( real bearing , coordinate coordinate )

Sets the bearing for the map to bearing , rotating it around coordinate . If the Plugin used for the Map supports bearing, the valid range for bearing is between 0 and 360. If the Plugin used for the Map does not support bearing, or if the map is tilted and coordinate happens to be behind the camera, or if the map is not ready (see mapReady ), calling this method will have no effect.

The release of this API with Qt 5.10 is a Technology Preview.

This method was introduced in Qt 5.10.

coordinate toCoordinate ( QPointF position , bool clipToViewPort )

Returns the coordinate which corresponds to the position relative to the map item.

clipToViewPort is true , or not supplied then returns an invalid coordinate if position is not within the current viewport.