OsmTileLayer class
A tile layer which renders map tiles provided by the OpenStreetMap project.
class OsmTileLayer extends TileLayer { OsmTileLayer({tileSources, renderer:CANVAS_RENDERER}) : super(renderer:renderer) { this.tileSources = tileSources; } List _tileSources; /// the list of tile sources this layer uses List get tileSources => new List.from(_tileSources); /** * Set the tile sources where tiles are loaded from. * * If more than one tile source is supplied, the layer loads map * tile in random order from them. * * ## Possible values for [sources] * * * [:null:] which results in an empty list of tile sources * * a [String] with a tile URL template * * a [List] of strings, a tile URL template each * * A *tile URL template* is a string which can include the following * macros: * * * `{z}` - bound to the current zoom level when a tile is loaded * * `{x}` - bound to the x-coordiate of the tile when a tile is loaded * * `{y}` - bound to the y-coordiate of the tile when a tile is loaded */ void set tileSources(sources) { if (sources == null) { _tileSources = null; } else if (sources is String) { _tileSources = new List.filled(1, sources); } else if (sources is List) { sources = sources.where((s) => s != null); _require(sources.every((s) => s is String), "Expected list tile source strings, got $sources"); _tileSources = new List.from(sources, growable: false); } } var _random = new math.Random(); /// selects a random tile source from the list of configured tile sources String get _randomTileSource { if (_tileSources == null) return null; if (_tileSources.length == 1) return _tileSources.first; return _tileSources[_random.nextInt(_tileSources.length)]; } static final _TEMPLATE_BIND_REGEXP = new RegExp(r"\{\s*(x|y|z)\s*\}"); @override String bindTileToUrl(int x, int y, int zoom) { var source = _randomTileSource; return source.splitMapJoin( _TEMPLATE_BIND_REGEXP, onMatch: (Match m) { switch(m.group(1).toLowerCase()) { case "x": return x.toString(); case "y": return y.toString(); case "z": return zoom.toString(); } }); } }
Extends
Object_PropertyObservable > Layer > TileLayer > OsmTileLayer
Constructors
new OsmTileLayer({tileSources, renderer: CANVAS_RENDERER}) #
OsmTileLayer({tileSources, renderer:CANVAS_RENDERER}) : super(renderer:renderer) { this.tileSources = tileSources; }
Properties
String get domId #
the unique layer id
String get domId => _container.attributes["id"];
void set domId(String value) #
Sets the DOM id on the layer container
.
If value is null or empty, sets a default id.
void set domId(String value) { if (value != null) value = value.trim(); if (value == null || value.isEmpty) { value = _defaultDOMId(); } _container.attributes["id"] = value; }
final MapViewport map #
the map this layer is attached to, or null
MapViewport get map => _map;
String get name #
the layer name
String get name { if (_name == null) return _defaultName; return _name; }
void set name(String value) #
sets the layer name. If value is null or consists of white space only, a default name is chosen.
void set name(String value) { if (value != null) value = value.trim(); var old = _name; if (value == null || value.isEmpty) { _name = null; } else { _name = value; } notify("name", old, name); }
final Stream<PropertyChangeEvent> onPropertyChanged #
the stream of property change events
Example
// an observable with a mixed in PropertyObservable
var observable = ...;
// listen for property change events for the property
// 'my_property'
observable.onPropertyChanged
.where((evt) => evt.name == "my_property")
.listen((evt) => print("new value: ${evt.newValue}"));
Stream<PropertyChangeEvent> get onPropertyChanged { //TODO: fix this - if at least one listener is present, // change events are emitted, regardless of whether the // individual listeners are paused or not. Consequence: // lots of change events are possibly queued up in // paused listener streams. => need a custom implementation // of a multiplexing stream which disards events if // they are streamed to a disabled listener // if (_stream == null) { _stream = _controller.stream.asBroadcastStream(); } return _stream; }
void set opacity(num value) #
set the opacity of this layer.
value is a num
in the range
(0.0 - 1.0). The lower the value, the more transparent the layer.
void set opacity(num value) { value = math.max(0, value); value = math.min(value, 1); var oldvalue = _opacity; this._opacity = value.toDouble(); if (oldvalue != this._opacity) { notify("opacity", oldvalue, this._opacity); if (_map != null) { _map.render(); } } }
final Dimension tileSize #
the tile size used by this tile layer
Dimension get tileSize => DEFAULT_TILE_SIZE;
List get tileSources #
the list of tile sources this layer uses
List get tileSources => new List.from(_tileSources);
void set tileSources(sources) #
Set the tile sources where tiles are loaded from.
If more than one tile source is supplied, the layer loads map tile in random order from them.
Possible values for sources
null
which results in an empty list of tile sources- a
String
with a tile URL template - a
List
of strings, a tile URL template each
A tile URL template is a string which can include the following macros:
{z}
- bound to the current zoom level when a tile is loaded{x}
- bound to the x-coordiate of the tile when a tile is loaded{y}
- bound to the y-coordiate of the tile when a tile is loaded
void set tileSources(sources) { if (sources == null) { _tileSources = null; } else if (sources is String) { _tileSources = new List.filled(1, sources); } else if (sources is List) { sources = sources.where((s) => s != null); _require(sources.every((s) => s is String), "Expected list tile source strings, got $sources"); _tileSources = new List.from(sources, growable: false); } }
void set visible(bool value) #
sets whether this layer is visible or not
void set visible(bool value) { var old = _visible; if (value != old) { if (_container != null) { _container.style.visibility = value ? "visible" : "hidden"; } _visible = value; notify("visible", old, value); } }
Methods
void attach(MapViewport m) #
Attaches the layer to a map viewport m.
Throws StateError
if this layer is already attached.
@override void attach(MapViewport m) { super.attach(m); var viewportSize = map.viewportSize; var tl = map.topLeftInPage; _container.style ..width="100%" ..height="100%" ..position = "absolute" ..left = "0px" ..top = "0px"; }
String bindTileToUrl(int x, int y, int zoom) #
returns a tile URL for the tile at tile coordinates x, y in the tile plane at zoom level zoom
@override String bindTileToUrl(int x, int y, int zoom) { var source = _randomTileSource; return source.splitMapJoin( _TEMPLATE_BIND_REGEXP, onMatch: (Match m) { switch(m.group(1).toLowerCase()) { case "x": return x.toString(); case "y": return y.toString(); case "z": return zoom.toString(); } }); }
void detach() #
Detaches this layer from the map viewport it is currently attached to.
void detach(){ _map = null; }
void notify(String property, oldValue, newValue) #
Notifies observers about an update of the property with name property in this object. oldValue was replaced by newValue.
Observers are only notified, provided newValue is different from oldValue and if there is at least one listener.
void notify(String property, oldValue, newValue) { if (oldValue == newValue) return; //TODO: fix me - see notes in onPropertyChanged if (!_controller.hasListener || _controller.isPaused) return; _controller.sink.add( new PropertyChangeEvent(this, property,oldValue,newValue) ); }