Skip to content

How to switch map layers #629

@S-Man42

Description

@S-Man42

Hi,

I have two map layers: OSM and MapBox Satellite:

 layers: [
   TileLayerOptions(
        urlTemplate: 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
        subdomains: ['a', 'b', 'c'],
        tileProvider: CachedNetworkTileProvider()
    ),
    TileLayerOptions(
        urlTemplate: 'https://api.mapbox.com/v4/mapbox.satellite/{z}/{x}/{y}@2x.jpg90?access_token={accessToken}',
        additionalOptions: {
          'accessToken': '...'
        },
        tileProvider: CachedNetworkTileProvider()
    )
  ]

Is there a way to add a layer switch to switch between both layers?

As workaround I tried to add a button with setState() in onPressed(). However, althought the build() is called, the underlying map is not changing:


_showSatellite = false;
...
layers: [
  _showSatellite ? TileLayerOptions(/*mapbox*/) : TileLayerOptions(/*osm*/)
]

...

Button(
  onPressed: () {
    setState() {
      _showSatellite = !_showSatellite;
    }
  }
)

Any help is appreciated :) Thanks in advance!

Edit: It seems that the layer changes. When I pan or zoom, the switch layer is shown. So, I need a way to refresh the currently shown tiles anyway...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions