|
| 1 | +// Copyright 2018 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:ui' show hashValues; |
| 6 | +import 'package:flutter/foundation.dart'; |
| 7 | + |
| 8 | +import 'types.dart'; |
| 9 | +import 'package:meta/meta.dart' show immutable, required; |
| 10 | + |
| 11 | +/// Uniquely identifies a [TileOverlay] among [GoogleMap] tile overlays. |
| 12 | +@immutable |
| 13 | +class TileOverlayId { |
| 14 | + /// Creates an immutable identifier for a [TileOverlay]. |
| 15 | + TileOverlayId(this.value) : assert(value != null); |
| 16 | + |
| 17 | + /// The value of the [TileOverlayId]. |
| 18 | + final String value; |
| 19 | + |
| 20 | + @override |
| 21 | + bool operator ==(Object other) { |
| 22 | + if (other.runtimeType != runtimeType) { |
| 23 | + return false; |
| 24 | + } |
| 25 | + return other is TileOverlayId && other.value == value; |
| 26 | + } |
| 27 | + |
| 28 | + @override |
| 29 | + int get hashCode => value.hashCode; |
| 30 | + |
| 31 | + @override |
| 32 | + String toString() => '${objectRuntimeType(this, 'TileOverlayId')}($value)'; |
| 33 | +} |
| 34 | + |
| 35 | +/// A set of images which are displayed on top of the base map tiles. |
| 36 | +/// |
| 37 | +/// These tiles may be transparent, allowing you to add features to existing maps. |
| 38 | +/// |
| 39 | +/// ## Tile Coordinates |
| 40 | +/// |
| 41 | +/// Note that the world is projected using the Mercator projection |
| 42 | +/// (see [Wikipedia](https://en.wikipedia.org/wiki/Mercator_projection)) with the left (west) side |
| 43 | +/// of the map corresponding to -180 degrees of longitude and the right (east) side of the map |
| 44 | +/// corresponding to 180 degrees of longitude. To make the map square, the top (north) side of the |
| 45 | +/// map corresponds to 85.0511 degrees of latitude and the bottom (south) side of the map |
| 46 | +/// corresponds to -85.0511 degrees of latitude. Areas outside this latitude range are not rendered. |
| 47 | +/// |
| 48 | +/// At each zoom level, the map is divided into tiles and only the tiles that overlap the screen are |
| 49 | +/// downloaded and rendered. Each tile is square and the map is divided into tiles as follows: |
| 50 | +/// |
| 51 | +/// * At zoom level 0, one tile represents the entire world. The coordinates of that tile are |
| 52 | +/// (x, y) = (0, 0). |
| 53 | +/// * At zoom level 1, the world is divided into 4 tiles arranged in a 2 x 2 grid. |
| 54 | +/// * ... |
| 55 | +/// * At zoom level N, the world is divided into 4N tiles arranged in a 2N x 2N grid. |
| 56 | +/// |
| 57 | +/// Note that the minimum zoom level that the camera supports (which can depend on various factors) |
| 58 | +/// is GoogleMap.getMinZoomLevel and the maximum zoom level is GoogleMap.getMaxZoomLevel. |
| 59 | +/// |
| 60 | +/// The coordinates of the tiles are measured from the top left (northwest) corner of the map. |
| 61 | +/// At zoom level N, the x values of the tile coordinates range from 0 to 2N - 1 and increase from |
| 62 | +/// west to east and the y values range from 0 to 2N - 1 and increase from north to south. |
| 63 | +/// |
| 64 | +class TileOverlay { |
| 65 | + /// Creates an immutable representation of a [TileOverlay] to draw on [GoogleMap]. |
| 66 | + const TileOverlay({ |
| 67 | + @required this.tileOverlayId, |
| 68 | + this.fadeIn = true, |
| 69 | + this.tileProvider, |
| 70 | + this.transparency = 0.0, |
| 71 | + this.zIndex, |
| 72 | + this.visible = true, |
| 73 | + this.tileSize = 256, |
| 74 | + }) : assert(transparency >= 0.0 && transparency <= 1.0); |
| 75 | + |
| 76 | + /// Uniquely identifies a [TileOverlay]. |
| 77 | + final TileOverlayId tileOverlayId; |
| 78 | + |
| 79 | + /// Whether the tiles should fade in. The default is true. |
| 80 | + final bool fadeIn; |
| 81 | + |
| 82 | + /// The tile provider to use for this tile overlay. |
| 83 | + final TileProvider tileProvider; |
| 84 | + |
| 85 | + /// The transparency of the tile overlay. The default transparency is 0 (opaque). |
| 86 | + final double transparency; |
| 87 | + |
| 88 | + /// The tile overlay's zIndex, i.e., the order in which it will be drawn where |
| 89 | + /// overlays with larger values are drawn above those with lower values |
| 90 | + final int zIndex; |
| 91 | + |
| 92 | + /// The visibility for the tile overlay. The default visibility is true. |
| 93 | + final bool visible; |
| 94 | + |
| 95 | + /// Specifies the number of logical pixels (not points) that the returned tile images will prefer |
| 96 | + /// to display as. iOS only. |
| 97 | + /// |
| 98 | + /// Defaults to 256, which is the traditional size of Google Maps tiles. |
| 99 | + /// As an example, an application developer may wish to provide retina tiles (512 pixel edge length) |
| 100 | + /// on retina devices, to keep the same number of tiles per view as the default value of 256 |
| 101 | + /// would give on a non-retina device. |
| 102 | + final int tileSize; |
| 103 | + |
| 104 | + /// Creates a new [TileOverlay] object whose values are the same as this instance, |
| 105 | + /// unless overwritten by the specified parameters. |
| 106 | + TileOverlay copyWith({ |
| 107 | + TileOverlayId tileOverlayId, |
| 108 | + bool fadeInParam, |
| 109 | + double transparencyParam, |
| 110 | + int zIndexParam, |
| 111 | + bool visibleParam, |
| 112 | + int tileSizeParam, |
| 113 | + }) { |
| 114 | + return TileOverlay( |
| 115 | + tileOverlayId: tileOverlayId, |
| 116 | + fadeIn: fadeInParam ?? fadeIn, |
| 117 | + transparency: transparencyParam ?? transparency, |
| 118 | + zIndex: zIndexParam ?? zIndex, |
| 119 | + visible: visibleParam ?? visible, |
| 120 | + tileSize: tileSizeParam ?? tileSize, |
| 121 | + ); |
| 122 | + } |
| 123 | + |
| 124 | + /// Converts this object to JSON. |
| 125 | + Map<String, dynamic> toJson() { |
| 126 | + final Map<String, dynamic> json = <String, dynamic>{}; |
| 127 | + |
| 128 | + void addIfPresent(String fieldName, dynamic value) { |
| 129 | + if (value != null) { |
| 130 | + json[fieldName] = value; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + addIfPresent('tileOverlayId', tileOverlayId.value); |
| 135 | + addIfPresent('fadeIn', fadeIn); |
| 136 | + addIfPresent('transparency', transparency); |
| 137 | + addIfPresent('zIndex', zIndex); |
| 138 | + addIfPresent('visible', visible); |
| 139 | + addIfPresent('tileSize', tileSize); |
| 140 | + |
| 141 | + return json; |
| 142 | + } |
| 143 | + |
| 144 | + @override |
| 145 | + bool operator ==(Object other) { |
| 146 | + if (other.runtimeType != runtimeType) { |
| 147 | + return false; |
| 148 | + } |
| 149 | + return other is TileOverlay && |
| 150 | + tileOverlayId == other.tileOverlayId && |
| 151 | + fadeIn == other.fadeIn && |
| 152 | + transparency == other.transparency && |
| 153 | + zIndex == other.zIndex && |
| 154 | + visible == other.visible && |
| 155 | + tileSize == other.tileSize; |
| 156 | + } |
| 157 | + |
| 158 | + @override |
| 159 | + int get hashCode => hashValues( |
| 160 | + tileOverlayId, fadeIn, transparency, zIndex, visible, tileSize); |
| 161 | +} |
0 commit comments