QML map proper markers instantiating
-
Hello.
I have a map(OpenStreetMaps) displayed using Map.qml ( Window { ... Map {...} } )
I have a Marker.qml file (based on MapQuickItem). All is OK, when I place this marker statically to my map Map { ... Marker { params } }.I want to place N markers (that, sure, have coords and other params). Markers params defined in somefile.txt and this file is parsed using C++.
The question is how to proper/faster/correctly instantiate and place parsed points to Map? Should I do method in Map.qml like a addMarker(position, name) { map.addMapItem( ? ) }?
Or should I place this marker using only C++, like get Map container and invoke addMapItem on some "map-projected" class?Please, help me to understand pipeline of this.
-
I have realized something similar with a
MapItemView
and a custom model (defined in C++ and exposed to QML).see http://doc.qt.io/qt-5/qml-qtlocation-mapitemview.html
My previous approach was based on
addMapItem
, but in my opinion a custom model is nicer, cleaner and also better maintainable...but that's just my personal opinion, I don't know if there are any actual performance differences. -
With your way I simply need to add child item to parent map Item?
-
I am currently not at home, so I can't provide you a code snippet, but what you would need to do is implement your own model by subclassing
QAbstractListModel
and providing alatitude
andlongitude
role. You can then either expose your model to QML and populate it there or expose the already populated model to QML.Have a look at https://github.com/a-team-fr/MeetupMobileQtQml/blob/master/160125/DemoProject/main.qml
In that example a XML Listmodel is used, but the principle is the same. -
Hi b2soft,
You can also take a look at the V-Play Map Example.
There, we demonstrate how to create markers from an XML file and create an XML List Model from it, which we use for our markers. See this QML file. Thus, If you decide to change your marker source from a simple .txt file to XML, you could use this approach as well.
Note that this example uses awesome components of V-Play Engine that simplify the task, but the concept should also be applicable for stock Qt apps.
Cheers,
Lorenz