Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to change map.activeMapType at the app startup?
QtWS25 Last Chance

How to change map.activeMapType at the app startup?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 1 Posters 258 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Atr0p0sA Offline
    Atr0p0sA Offline
    Atr0p0s
    wrote on last edited by Atr0p0s
    #1

    I want to change map.activeMapType at the app startup. I have menu mapTypeMenu with actions that change activeMapType. So I added Component.onCompleted to the MapView to trigger the needed action to switch activeMapType. But it looks like map switches to the default (street map) activeMapType again after this.

    ApplicationWindow {
    MapView {
            id: mapview
            anchors.fill: parent
            map.plugin: Plugin { name: "osm" }
            map.zoomLevel: 4
    
            Component.onCompleted: {
                for (var i = 0; i < mapTypeMenu.count; i++) {
                    if (mapTypeMenu.actionAt(i).text == "Terrain Map") {
                        console.log("triggered")
                        mapTypeMenu.actionAt(i).trigger();
                        console.log(mapTypeMenu.actionAt(i).text)
                    }
                }
                console.log("map compl")
            }
    }
    }
    

    When action from mapTypeMenu triggers, called following code:

    onSelectMapType: (mapType) => {
        for (var i = 0; i < mapTypeMenu.count; i++) { 
           mapTypeMenu.actionAt(i).checked = mapTypeMenu.actionAt(i).text === mapType.name
        }
        mapview.map.activeMapType = mapType
        console.log("Done")
    }
    

    Output:

    qml: window compl
    qml: triggered
    qml: Done
    qml: Terrain Map
    qml: map compl
    QGeoTileProviderOsm: Tileserver disabled at  QUrl("http://maps-redirect.qt.io/osm/5.8/satellite")
    QGeoTileFetcherOsm: all providers resolved
    

    Can you help to solve the problem please?

    Atr0p0sA 1 Reply Last reply
    0
    • Atr0p0sA Atr0p0s

      Solve the problem by adding logic to the Timer:

          Timer {
              interval: 2
              onTriggered: {
                  for (var i = 0; i < mapTypeMenu.count; i++) {
                      if (mapTypeMenu.actionAt(i).text === "Terrain Map") {
                          mapTypeMenu.actionAt(i).trigger();
                          console.log("Triggered: " + mapTypeMenu.actionAt(i).text);
                      }
                  }
              }
              running: true
              repeat: false
          }
      

      But is there a better solution?

      Atr0p0sA Offline
      Atr0p0sA Offline
      Atr0p0s
      wrote on last edited by
      #3

      Found some solution here.

      1 Reply Last reply
      0
      • Atr0p0sA Atr0p0s

        I want to change map.activeMapType at the app startup. I have menu mapTypeMenu with actions that change activeMapType. So I added Component.onCompleted to the MapView to trigger the needed action to switch activeMapType. But it looks like map switches to the default (street map) activeMapType again after this.

        ApplicationWindow {
        MapView {
                id: mapview
                anchors.fill: parent
                map.plugin: Plugin { name: "osm" }
                map.zoomLevel: 4
        
                Component.onCompleted: {
                    for (var i = 0; i < mapTypeMenu.count; i++) {
                        if (mapTypeMenu.actionAt(i).text == "Terrain Map") {
                            console.log("triggered")
                            mapTypeMenu.actionAt(i).trigger();
                            console.log(mapTypeMenu.actionAt(i).text)
                        }
                    }
                    console.log("map compl")
                }
        }
        }
        

        When action from mapTypeMenu triggers, called following code:

        onSelectMapType: (mapType) => {
            for (var i = 0; i < mapTypeMenu.count; i++) { 
               mapTypeMenu.actionAt(i).checked = mapTypeMenu.actionAt(i).text === mapType.name
            }
            mapview.map.activeMapType = mapType
            console.log("Done")
        }
        

        Output:

        qml: window compl
        qml: triggered
        qml: Done
        qml: Terrain Map
        qml: map compl
        QGeoTileProviderOsm: Tileserver disabled at  QUrl("http://maps-redirect.qt.io/osm/5.8/satellite")
        QGeoTileFetcherOsm: all providers resolved
        

        Can you help to solve the problem please?

        Atr0p0sA Offline
        Atr0p0sA Offline
        Atr0p0s
        wrote on last edited by
        #2

        Solve the problem by adding logic to the Timer:

            Timer {
                interval: 2
                onTriggered: {
                    for (var i = 0; i < mapTypeMenu.count; i++) {
                        if (mapTypeMenu.actionAt(i).text === "Terrain Map") {
                            mapTypeMenu.actionAt(i).trigger();
                            console.log("Triggered: " + mapTypeMenu.actionAt(i).text);
                        }
                    }
                }
                running: true
                repeat: false
            }
        

        But is there a better solution?

        Atr0p0sA 1 Reply Last reply
        0
        • Atr0p0sA Atr0p0s

          Solve the problem by adding logic to the Timer:

              Timer {
                  interval: 2
                  onTriggered: {
                      for (var i = 0; i < mapTypeMenu.count; i++) {
                          if (mapTypeMenu.actionAt(i).text === "Terrain Map") {
                              mapTypeMenu.actionAt(i).trigger();
                              console.log("Triggered: " + mapTypeMenu.actionAt(i).text);
                          }
                      }
                  }
                  running: true
                  repeat: false
              }
          

          But is there a better solution?

          Atr0p0sA Offline
          Atr0p0sA Offline
          Atr0p0s
          wrote on last edited by
          #3

          Found some solution here.

          1 Reply Last reply
          0
          • Atr0p0sA Atr0p0s has marked this topic as solved on

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved