Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Dynamic objects Adding/removal with Qt Quick Map API

From Wiki
Jump to: navigation, search
Article Metadata
Created: symbianyucca (04 Oct 2011)
Last edited: hamishwillee (24 Apr 2013)

With QML Map element you can add following items to the Map element:

  • MapPolygon: for general polygon objects
  • MapPolyline: for general polyline objects
  • MapCircle: for Circle objects (polygon)
  • MapRectangle: for Rectangle objects (polygon)
  • MapText: for textual map objects
  • MapImage: for Icon (marker/pushpin) objects.

Basically if you know the elements to be shown on coding time, you can easily define the objects inside the Map element and use the visible variable to set them hidden/visible. This would mean that no actual dynamic handling is required. Here’s simple code example on toggling the visibility of a polyline objec

polylines.visible = false;
polylines.visible = true;

Full examples on this kind of content handling can be found from QML‎ and QML‎ examples.

You can also manage map objects in groups by using the MapGroup element. Note though that with some versions there is a bug that prevents the visible variable working for MapGroup. The workaround is to use the objects array, and to loop the items inside the MapGroup one by one and set their visibility individually:

for (var i = 0; i < icons.objects.length; ++i) {
icons.objects[i].visible = icons.visible;

Full example for the workaround is also shown in QML‎ example with MapImages inside a MapGroup.

If the objects need to be specified on run time, or if real dynamic handling of the content objects is required, you can use the addMapObject/removeMapObject functions defined for the Map element. Already added objects should be visible in objects, though note a bug effecting some versions, where the objects is not defined, and thus can not be used.

One possible workaround for the missing objects array, is to define own javascript array and use it to track the objects added to the Map. For example adding a new circle object into the Map could be handled as follows:

var newCircle = Qt.createQmlObject('import QtQuick 1.1;import QtMobility.location 1.2; MapCircle {color: "#40FF0000"; radius: 3000; }',map, "dynamicSnippet1"); = map.toCoordinate(Qt.point(mouse.x,(mouse.y - titleBar.height)));

Removing of the object could then be handled for example like this


With the snippeds the "Script.myCirclesArray" is javascript array. Full example for the workaround can be found from QML example.

This page was last modified on 24 April 2013, at 03:24.
124 page views in the last 30 days.