11// Import source types, layer types, and formats.
22import VectorSource from 'ol/source/Vector' ;
33import Cluster from 'ol/source/Cluster' ;
4+ import Google from 'ol/source/Google' ;
45import TileArcGISRest from 'ol/source/TileArcGISRest' ;
56import TileWMS from 'ol/source/TileWMS' ;
67import XYZ from 'ol/source/XYZ' ;
@@ -19,6 +20,9 @@ import colorStyles, { clusterStyle } from '../../styles';
1920// Import readFeatures function.
2021import readFeatures from './features' ;
2122
23+ // Import GoogleLogoAttribution control.
24+ import GoogleLogoAttribution from '../../control/Google/GoogleLogoAttribution' ;
25+
2226// Set withCredentials to true for all XHR requests made via OpenLayers'
2327// feature loader. Typically farmOS requires authentication in order to
2428// retrieve data from its GeoJSON endpoints. Setting withCredentials to true
@@ -103,6 +107,29 @@ function addGeoJSONLayer({
103107 return layer ;
104108}
105109
110+ // Add a Google Map Tiles layer to the map.
111+ function addGoogleMapTilesLayer ( {
112+ title = 'google-map-tiles' , key, mapType = 'satellite' , layerTypes = [ ] , language = 'en-US' , region = 'US' , apiOptions = null , visible = true , base = true ,
113+ } ) {
114+ const source = new Google ( {
115+ key,
116+ mapType,
117+ layerTypes,
118+ language,
119+ region,
120+ apiOptions,
121+ scale : 'scaleFactor2x' ,
122+ highDpi : true ,
123+ } ) ;
124+ const layer = new TileLayer ( {
125+ title,
126+ source,
127+ visible,
128+ type : base ? 'base' : 'normal' ,
129+ } ) ;
130+ return layer ;
131+ }
132+
106133// Add a Tile ArcGIS MapServer layer to the map.
107134function addTileArcGISMapServerLayer ( {
108135 title = 'arcgis-tile' , url, params, visible = true , base = false , attribution = '' , crossOrigin = null ,
@@ -226,6 +253,16 @@ export default function addLayer(type, opts = {}) {
226253 }
227254 layer = addGeoJSONLayer ( opts ) ;
228255 }
256+ if ( type . toLowerCase ( ) === 'google' ) {
257+ if ( ! opts . key ) {
258+ throw new Error ( 'Missing a Google Map Tiles API key.' ) ;
259+ }
260+ layer = addGoogleMapTilesLayer ( opts ) ;
261+ if ( ! this . map . getControls ( ) . getArray ( )
262+ . some ( control => control instanceof GoogleLogoAttribution ) ) {
263+ this . map . addControl ( new GoogleLogoAttribution ( ) ) ;
264+ }
265+ }
229266 if ( type . toLowerCase ( ) === 'arcgis-tile' ) {
230267 if ( ! opts . url ) {
231268 throw new Error ( 'Missing a ArcGIS MapServer url.' ) ;
0 commit comments