<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- minimal_map.qdoc --> <title>Minimal Map (QML) | Qt Location 5.12.6</title> <link rel="stylesheet" type="text/css" href="style/offline-simple.css" /> <script type="text/javascript"> document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css"); // loading style sheet breaks anchors that were jumped to before // so force jumping to anchor again setTimeout(function() { var anchor = location.hash; // need to jump to different anchor first (e.g. none) location.hash = "#"; setTimeout(function() { location.hash = anchor; }, 0); }, 0); </script> </head> <body> <div class="header" id="qtdocheader"> <div class="main"> <div class="main-rounded"> <div class="navigationbar"> <table><tr> <td >Qt 5.12</td><td ><a href="qtlocation-index.html">Qt Location</a></td><td ><a href="qtlocation-examples.html">Qt Location Examples</a></td><td >Minimal Map (QML)</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right"><a href="qtlocation-index.html">Qt 5.12.6 Reference Documentation</a></td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <div class="sidebar"> <div class="toc"> <h3><a name="toc">Contents</a></h3> <ul> <li class="level1"><a href="#running-the-example">Running the Example</a></li> <li class="level1"><a href="#c-code">C++ Code</a></li> <li class="level1"><a href="#qml-code">QML Code</a></li> <li class="level1"><a href="#requirements">Requirements</a></li> </ul> </div> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">Minimal Map (QML)</h1> <span class="subtitle"></span> <!-- $$$minimal_map-brief --> <p>The minimum code to display a map using Qt Quick.</p> <!-- @@@minimal_map --> <!-- $$$minimal_map-description --> <div class="descr"> <a name="details"></a> <p class="centerAlign"><img src="images/minimal_map.png" alt="" /></p><p><i>Minimal Map</i> demonstrates how to use the <a href="qml-qtlocation-map.html">Map</a> item to render a map. It shows the minimum amount of code needed to display the map, and can be used as a basis for further experimentation.</p> <a name="running-the-example"></a> <h2 id="running-the-example">Running the Example</h2> <p>To run the example from Qt Creator, open the <b>Welcome</b> mode and select the example from <b>Examples</b>. For more information, visit Building and Running an Example.</p> <a name="c-code"></a> <h2 id="c-code">C++ Code</h2> <p>In <code>main.cpp</code> we use only the QGuiApplication and QQmlApplicationEngine classes.</p> <pre class="cpp"> <span class="preprocessor">#include <QGuiApplication></span> <span class="preprocessor">#include <QQmlApplicationEngine></span> </pre> <p>In the main function, we first instantiate a QGuiApplication object. Then we create a QQmlApplicationEngine and tell it to load <code>main.qml</code> from the Qt Resource System.</p> <p>Finally, QGuiApplication::exec() launches the main event loop.</p> <pre class="cpp"> <span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span>argv<span class="operator">[</span><span class="operator">]</span>) { <span class="type">QGuiApplication</span> app(argc<span class="operator">,</span> argv); <span class="type">QQmlApplicationEngine</span> engine; engine<span class="operator">.</span>load(<span class="type">QUrl</span>(<span class="type">QStringLiteral</span>(<span class="string">"qrc:/main.qml"</span>))); <span class="keyword">return</span> app<span class="operator">.</span>exec(); } </pre> <a name="qml-code"></a> <h2 id="qml-code">QML Code</h2> <p>In <code>main.qml</code>, we import the <a href="qtlocation-qmlmodule.html">QtLocation</a> QML module and its depending QtPositioning QML module. Next, we create the top level window, set a sensible default size, and make it visible. The window will be filled by a <a href="qml-qtlocation-map.html">Map</a> item showing the map.</p> <pre class="cpp"> import QtQuick 2.0 import QtQuick.Window 2.0 import QtLocation 5.6 import QtPositioning 5.6 Window { width: 512 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 } } </pre> <p>The <a href="qml-qtlocation-plugin.html">Plugin</a> item is necessary to define the map provider we are going to use. The example can work with any of the available geo services plugins. However, some plugins may require additional plugin parameters in order to function correctly and we can use <a href="qml-qtlocation-pluginparameter.html">PluginParameter</a> to specify them. In this example, we use the <code>osm</code> plugin, which is a <a href="location-plugin-osm.html">Qt Location Open Street Map Plugin</a> and does not require any parameters.</p> <p>In the <a href="qml-qtlocation-map.html">Map</a> item, we refer to the <code>plugin</code> we use and we set the <code>center</code> and the <code>zoomLevel</code> of the map.</p> <a name="requirements"></a> <h2 id="requirements">Requirements</h2> <p>The example requires a working internet connection to download <code>OpenStreetMap</code> map tiles. An optional system proxy should be picked up automatically.</p> <p>Files:</p> <ul> <li><a href="qtlocation-minimal-map-main-cpp.html">minimal_map/main.cpp</a></li> <li><a href="qtlocation-minimal-map-main-qml.html">minimal_map/main.qml</a></li> <li><a href="qtlocation-minimal-map-minimal-map-pro.html">minimal_map/minimal_map.pro</a></li> <li><a href="qtlocation-minimal-map-qml-qrc.html">minimal_map/qml.qrc</a></li> </ul> </div> <!-- @@@minimal_map --> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2019 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.<br/> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br/> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. </p> </div> </body> </html>