Map External Layer
Add your own layers on top of Woosmap Map.
Example
Map External Layer
let map: woosmap.map.Map;
let styleSelect, layerSelect: HTMLSelectElement;
let opacityInput: HTMLInputElement;
const centerLatLng: woosmap.map.LatLngLiteral = {
lat: 39.15253,
lng: -97.74332
};
function initMap() {
map = new window.woosmap.map.Map(
document.getElementById("map") as HTMLElement,
{
zoom: 3,
center: centerLatLng,
styles: []
},
);
const imageMapType = new woosmap.map.ImageMapType({
url: "https://services.arcgisonline.com/arcgis/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}.png",
tileSize: new woosmap.map.Size(256, 256),
maxZoom: 19
});
map.overlayMapTypes.insertAt(0, imageMapType);
const onChangeHandler = () => {
if (
parseFloat(opacityInput.value) > 1 ||
parseFloat(opacityInput.value) < 0
) {
alert("Value should be between 0 and 1");
return;
}
changeStyle(styleSelect.value)
changeLayer(layerSelect.value, parseFloat(opacityInput.value))
};
styleSelect = document.getElementById(
"woosmap-style-select",
) as HTMLSelectElement;
layerSelect = document.getElementById(
"layer-provider-select",
) as HTMLSelectElement;
opacityInput = document.getElementById(
"opacity-value-input",
) as HTMLInputElement;
styleSelect.addEventListener("change", onChangeHandler);
layerSelect.addEventListener("change", onChangeHandler);
opacityInput.addEventListener("change", onChangeHandler);
}
function changeStyle(name: String)
{
let styles;
if(name == "retro")
{
styles = retroStyles
}
else if(name == "lightgrey")
{
styles = lightgreyStyles
}
else if(name=="night")
{
styles = nightStyles
}
else{
styles = []
}
map = new window.woosmap.map.Map(
document.getElementById("map") as HTMLElement,
{
zoom: map.getZoom(),
center: map.getCenter(),
styles: styles
},
);
}
function changeLayer(provider: string, opacity: number)
{
let url
if(provider == "temperature")
{
url = `https://tile.openweathermap.org/map/temp_new/{z}/{x}/{y}?appid=${openWeatherMapKey}`;
}
else if(provider == "precipitation")
{
url = `https://tile.openweathermap.org/map/precipitation_new/{z}/{x}/{y}?appid=${openWeatherMapKey}`;
}
else
{
url = `https://services.arcgisonline.com/arcgis/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}.png`;
}
const imageMapType = new woosmap.map.ImageMapType({
url: url,
tileSize: new woosmap.map.Size(256, 256),
maxZoom: 19,
opacity: opacity,
name: "Layer"
});
map.overlayMapTypes.insertAt(0, imageMapType);
}
const nightStyles = [
{
elementType: "geometry",
stylers: [{ color: "#242f3e" }],
},
{
elementType: "labels.text.stroke",
stylers: [{ color: "#242f3e" }],
},
{
elementType: "labels.text.fill",
stylers: [{ color: "#746855" }],
},
{
featureType: "administrative.locality",
elementType: "labels.text.fill",
stylers: [{ color: "#d59563" }],
},
{
featureType: "poi",
elementType: "labels.text.fill",
stylers: [{ color: "#d59563" }],
},
{
featureType: "poi.park",
elementType: "geometry",
stylers: [{ color: "#326030" }],
},
{
featureType: "poi.park",
elementType: "labels.text.fill",
stylers: [{ color: "#6b9a76" }],
},
{
featureType: "road",
elementType: "geometry",
stylers: [{ color: "#38414e" }],
},
{
featureType: "road",
elementType: "geometry.stroke",
stylers: [{ color: "#212a37" }],
},
{
featureType: "road",
elementType: "labels.text.fill",
stylers: [{ color: "#9ca5b3" }],
},
{
featureType: "road.highway",
elementType: "geometry",
stylers: [{ color: "#746855" }],
},
{
featureType: "road.highway",
elementType: "geometry.stroke",
stylers: [{ color: "#1f2835" }],
},
{
featureType: "road.highway",
elementType: "labels.text.fill",
stylers: [{ color: "#C38B5F" }],
},
{
featureType: "transit",
elementType: "geometry",
stylers: [{ color: "#2f3948" }],
},
{
featureType: "transit.station",
elementType: "labels.text.fill",
stylers: [{ color: "#d59563" }],
},
{
featureType: "water",
elementType: "geometry",
stylers: [{ color: "#17263c" }],
},
{
featureType: "water",
elementType: "labels.text.fill",
stylers: [{ color: "#515c6d" }],
},
{
featureType: "water",
elementType: "labels.text.stroke",
stylers: [{ color: "#17263c" }],
},
]
const retroStyles = [
{
elementType: "geometry",
stylers: [{ color: "#ebe3cd" }],
},
{
elementType: "labels.text.fill",
stylers: [{ color: "#523735" }],
},
{
elementType: "labels.text.stroke",
stylers: [{ color: "#f5f1e6" }],
},
{
featureType: "administrative",
elementType: "geometry.stroke",
stylers: [{ color: "#c9b2a6" }],
},
{
featureType: "administrative.land_parcel",
elementType: "geometry.stroke",
stylers: [{ color: "#dcd2be" }],
},
{
featureType: "administrative.land_parcel",
elementType: "labels.text.fill",
stylers: [{ color: "#ae9e90" }],
},
{
featureType: "landscape.natural",
elementType: "geometry",
stylers: [{ color: "#dfd2ae" }],
},
{
featureType: "poi",
elementType: "geometry",
stylers: [{ color: "#dfd2ae" }],
},
{
featureType: "poi",
elementType: "labels.text.fill",
stylers: [{ color: "#93817c" }],
},
{
featureType: "poi.park",
elementType: "geometry",
stylers: [{ color: "#738E00" }],
},
{
featureType: "poi.park",
elementType: "labels.text.fill",
stylers: [{ color: "#447530" }],
},
{
featureType: "road",
elementType: "geometry",
stylers: [{ color: "#f5f1e6" }],
},
{
featureType: "road.arterial",
elementType: "geometry",
stylers: [{ color: "#fdfcf8" }],
},
{
featureType: "road.highway",
elementType: "geometry",
stylers: [{ color: "#f8c967" }],
},
{
featureType: "road.highway",
elementType: "geometry.stroke",
stylers: [{ color: "#e9bc62" }],
},
{
featureType: "road.highway.controlled_access",
elementType: "geometry",
stylers: [{ color: "#e98d58" }],
},
{
featureType: "road.highway.controlled_access",
elementType: "geometry.stroke",
stylers: [{ color: "#db8555" }],
},
{
featureType: "road.local",
elementType: "labels.text.fill",
stylers: [{ color: "#806b63" }],
},
{
featureType: "road.local",
elementType: "geometry.fill",
stylers: [{ color: "#F5F1E6" }],
},
{
featureType: "transit.line",
elementType: "geometry",
stylers: [{ color: "#dfd2ae" }],
},
{
featureType: "transit.line",
elementType: "labels.text.fill",
stylers: [{ color: "#8f7d77" }],
},
{
featureType: "transit.line",
elementType: "labels.text.stroke",
stylers: [{ color: "#ebe3cd" }],
},
{
featureType: "transit.station",
elementType: "geometry",
stylers: [{ color: "#dfd2ae" }],
},
{
featureType: "water",
elementType: "geometry.fill",
stylers: [{ color: "#b9d3c2" }],
},
{
featureType: "water",
elementType: "labels.text.fill",
stylers: [{ color: "#92998d" }],
},
]
const lightgreyStyles = [
{
elementType: "geometry",
stylers: [{ color: "#f6f6f6" }],
},
{
elementType: "labels.icon",
stylers: [{ visibility: "off" }],
},
{
elementType: "labels.text.fill",
stylers: [{ color: "#616161" }],
},
{
elementType: "labels.text.stroke",
stylers: [{ color: "#f5f5f5" }],
},
{
featureType: "administrative.land_parcel",
elementType: "labels.text.fill",
stylers: [{ color: "#bdbdbd" }],
},
{
featureType: "road",
elementType: "geometry",
stylers: [{ color: "#ffffff" }],
},
{
featureType: "road.arterial",
elementType: "labels.text.fill",
stylers: [{ color: "#757575" }],
},
{
featureType: "road.highway",
elementType: "geometry",
stylers: [{ color: "#dadada" }],
},
{
featureType: "road.highway",
elementType: "labels.text.fill",
stylers: [{ color: "#616161" }],
},
{
featureType: "road.local",
elementType: "labels.text.fill",
stylers: [{ color: "#9e9e9e" }],
},
{
featureType: "transit.line",
elementType: "geometry",
stylers: [{ color: "#e5e5e5" }],
},
{
featureType: "transit.station",
elementType: "geometry",
stylers: [{ color: "#eeeeee" }],
},
{
featureType: "water",
elementType: "geometry",
stylers: [{ color: "#c9c9c9" }],
},
{
featureType: "water",
elementType: "labels.text.fill",
stylers: [{ color: "#9e9e9e" }],
},
]
declare global {
interface Window {
initMap: () => void;
}
}
window.initMap = initMap;
let map;
let styleSelect, layerSelect;
let opacityInput;
const centerLatLng = {
lat: 39.15253,
lng: -97.74332,
};
function initMap() {
map = new window.woosmap.map.Map(document.getElementById("map"), {
zoom: 3,
center: centerLatLng,
styles: [],
});
const imageMapType = new woosmap.map.ImageMapType({
url: "https://services.arcgisonline.com/arcgis/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}.png",
tileSize: new woosmap.map.Size(256, 256),
maxZoom: 19,
});
map.overlayMapTypes.insertAt(0, imageMapType);
const onChangeHandler = () => {
if (
parseFloat(opacityInput.value) > 1 ||
parseFloat(opacityInput.value) < 0
) {
alert("Value should be between 0 and 1");
return;
}
changeStyle(styleSelect.value);
changeLayer(layerSelect.value, parseFloat(opacityInput.value));
};
styleSelect = document.getElementById("woosmap-style-select");
layerSelect = document.getElementById("layer-provider-select");
opacityInput = document.getElementById("opacity-value-input");
styleSelect.addEventListener("change", onChangeHandler);
layerSelect.addEventListener("change", onChangeHandler);
opacityInput.addEventListener("change", onChangeHandler);
}
function changeStyle(name) {
let styles;
if (name == "retro") {
styles = retroStyles;
} else if (name == "lightgrey") {
styles = lightgreyStyles;
} else if (name == "night") {
styles = nightStyles;
} else {
styles = [];
}
map = new window.woosmap.map.Map(document.getElementById("map"), {
zoom: map.getZoom(),
center: map.getCenter(),
styles: styles,
});
}
function changeLayer(provider, opacity) {
let url;
if (provider == "temperature") {
url = `https://tile.openweathermap.org/map/temp_new/{z}/{x}/{y}?appid=${openWeatherMapKey}`;
} else if (provider == "precipitation") {
url = `https://tile.openweathermap.org/map/precipitation_new/{z}/{x}/{y}?appid=${openWeatherMapKey}`;
} else {
url = `https://services.arcgisonline.com/arcgis/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}.png`;
}
const imageMapType = new woosmap.map.ImageMapType({
url: url,
tileSize: new woosmap.map.Size(256, 256),
maxZoom: 19,
opacity: opacity,
name: "Layer",
});
map.overlayMapTypes.insertAt(0, imageMapType);
}
const nightStyles = [
{
elementType: "geometry",
stylers: [{ color: "#242f3e" }],
},
{
elementType: "labels.text.stroke",
stylers: [{ color: "#242f3e" }],
},
{
elementType: "labels.text.fill",
stylers: [{ color: "#746855" }],
},
{
featureType: "administrative.locality",
elementType: "labels.text.fill",
stylers: [{ color: "#d59563" }],
},
{
featureType: "poi",
elementType: "labels.text.fill",
stylers: [{ color: "#d59563" }],
},
{
featureType: "poi.park",
elementType: "geometry",
stylers: [{ color: "#326030" }],
},
{
featureType: "poi.park",
elementType: "labels.text.fill",
stylers: [{ color: "#6b9a76" }],
},
{
featureType: "road",
elementType: "geometry",
stylers: [{ color: "#38414e" }],
},
{
featureType: "road",
elementType: "geometry.stroke",
stylers: [{ color: "#212a37" }],
},
{
featureType: "road",
elementType: "labels.text.fill",
stylers: [{ color: "#9ca5b3" }],
},
{
featureType: "road.highway",
elementType: "geometry",
stylers: [{ color: "#746855" }],
},
{
featureType: "road.highway",
elementType: "geometry.stroke",
stylers: [{ color: "#1f2835" }],
},
{
featureType: "road.highway",
elementType: "labels.text.fill",
stylers: [{ color: "#C38B5F" }],
},
{
featureType: "transit",
elementType: "geometry",
stylers: [{ color: "#2f3948" }],
},
{
featureType: "transit.station",
elementType: "labels.text.fill",
stylers: [{ color: "#d59563" }],
},
{
featureType: "water",
elementType: "geometry",
stylers: [{ color: "#17263c" }],
},
{
featureType: "water",
elementType: "labels.text.fill",
stylers: [{ color: "#515c6d" }],
},
{
featureType: "water",
elementType: "labels.text.stroke",
stylers: [{ color: "#17263c" }],
},
];
const retroStyles = [
{
elementType: "geometry",
stylers: [{ color: "#ebe3cd" }],
},
{
elementType: "labels.text.fill",
stylers: [{ color: "#523735" }],
},
{
elementType: "labels.text.stroke",
stylers: [{ color: "#f5f1e6" }],
},
{
featureType: "administrative",
elementType: "geometry.stroke",
stylers: [{ color: "#c9b2a6" }],
},
{
featureType: "administrative.land_parcel",
elementType: "geometry.stroke",
stylers: [{ color: "#dcd2be" }],
},
{
featureType: "administrative.land_parcel",
elementType: "labels.text.fill",
stylers: [{ color: "#ae9e90" }],
},
{
featureType: "landscape.natural",
elementType: "geometry",
stylers: [{ color: "#dfd2ae" }],
},
{
featureType: "poi",
elementType: "geometry",
stylers: [{ color: "#dfd2ae" }],
},
{
featureType: "poi",
elementType: "labels.text.fill",
stylers: [{ color: "#93817c" }],
},
{
featureType: "poi.park",
elementType: "geometry",
stylers: [{ color: "#738E00" }],
},
{
featureType: "poi.park",
elementType: "labels.text.fill",
stylers: [{ color: "#447530" }],
},
{
featureType: "road",
elementType: "geometry",
stylers: [{ color: "#f5f1e6" }],
},
{
featureType: "road.arterial",
elementType: "geometry",
stylers: [{ color: "#fdfcf8" }],
},
{
featureType: "road.highway",
elementType: "geometry",
stylers: [{ color: "#f8c967" }],
},
{
featureType: "road.highway",
elementType: "geometry.stroke",
stylers: [{ color: "#e9bc62" }],
},
{
featureType: "road.highway.controlled_access",
elementType: "geometry",
stylers: [{ color: "#e98d58" }],
},
{
featureType: "road.highway.controlled_access",
elementType: "geometry.stroke",
stylers: [{ color: "#db8555" }],
},
{
featureType: "road.local",
elementType: "labels.text.fill",
stylers: [{ color: "#806b63" }],
},
{
featureType: "road.local",
elementType: "geometry.fill",
stylers: [{ color: "#F5F1E6" }],
},
{
featureType: "transit.line",
elementType: "geometry",
stylers: [{ color: "#dfd2ae" }],
},
{
featureType: "transit.line",
elementType: "labels.text.fill",
stylers: [{ color: "#8f7d77" }],
},
{
featureType: "transit.line",
elementType: "labels.text.stroke",
stylers: [{ color: "#ebe3cd" }],
},
{
featureType: "transit.station",
elementType: "geometry",
stylers: [{ color: "#dfd2ae" }],
},
{
featureType: "water",
elementType: "geometry.fill",
stylers: [{ color: "#b9d3c2" }],
},
{
featureType: "water",
elementType: "labels.text.fill",
stylers: [{ color: "#92998d" }],
},
];
const lightgreyStyles = [
{
elementType: "geometry",
stylers: [{ color: "#f6f6f6" }],
},
{
elementType: "labels.icon",
stylers: [{ visibility: "off" }],
},
{
elementType: "labels.text.fill",
stylers: [{ color: "#616161" }],
},
{
elementType: "labels.text.stroke",
stylers: [{ color: "#f5f5f5" }],
},
{
featureType: "administrative.land_parcel",
elementType: "labels.text.fill",
stylers: [{ color: "#bdbdbd" }],
},
{
featureType: "road",
elementType: "geometry",
stylers: [{ color: "#ffffff" }],
},
{
featureType: "road.arterial",
elementType: "labels.text.fill",
stylers: [{ color: "#757575" }],
},
{
featureType: "road.highway",
elementType: "geometry",
stylers: [{ color: "#dadada" }],
},
{
featureType: "road.highway",
elementType: "labels.text.fill",
stylers: [{ color: "#616161" }],
},
{
featureType: "road.local",
elementType: "labels.text.fill",
stylers: [{ color: "#9e9e9e" }],
},
{
featureType: "transit.line",
elementType: "geometry",
stylers: [{ color: "#e5e5e5" }],
},
{
featureType: "transit.station",
elementType: "geometry",
stylers: [{ color: "#eeeeee" }],
},
{
featureType: "water",
elementType: "geometry",
stylers: [{ color: "#c9c9c9" }],
},
{
featureType: "water",
elementType: "labels.text.fill",
stylers: [{ color: "#9e9e9e" }],
},
];
window.initMap = initMap;
/*
* Always set the map height explicitly to define the size of the div element
* that contains the map.
*/
#map {
height: 100%;
}
/*
* Optional: Makes the sample page fill the window.
*/
html,
body {
height: 100%;
margin: 0;
padding: 0;
font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Helvetica Neue, Arial, Noto Sans, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol, Noto Color Emoji;
}
#app {
height: 100%;
}
#selector {
position: absolute;
top: 0px;
left: 0;
background-color: #fff;
border-radius: 3px;
box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.1);
margin: 10px;
padding: 5px;
overflow: hidden;
z-index: 1;
}
select {
width: 100px;
}
input {
width: 92px;
}
.select-container {
padding: 2px 0;
width: 100%;
display: flex;
justify-content: space-between;
}
#selector span {
padding: 0 5px;
}
<html>
<head>
<title>Map External Layer</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="./style.css" />
<script type="module" src="./index.js"></script>
</head>
<body>
<div id="app">
<div id="selector">
<div class="select-container">
<span>Woosmap Style </span>
<select id="woosmap-style-select">
<option value="default">Default</option>
<option value="lightgrey">Light Grey</option>
<option value="night">Transit By Night</option>
<option value="retro">Retro</option>
</select>
</div>
<div class="select-container">
<span>Third party layer </span>
<select id="layer-provider-select">
<option value="satellite">ArcGIS Satellite view</option>
<option value="temperature">OpenWeather temperature</option>
<option value="precipitation">OpenWeather precipitation</option>
</select>
</div>
<div class="select-container">
<span>Opacity </span>
<input
id="opacity-value-input"
type="number"
value="1"
step="0.1"
min="0"
max="1"
/>
</div>
</div>
<div id="map"></div>
</div>
<script
src="https://sdk.woosmap.com/map/map.js?key=woos-48c80350-88aa-333e-835a-07f4b658a9a4&callback=initMap"
defer
></script>
</body>
</html>
Running the Sample Locally
Before you can run this sample on your local machine, you need to have Git and Node.js installed. If they’re not already installed, follow these instructions to get them set up.
Once you have Git and Node.js installed, you can run the sample by following these steps:
-
Clone the repository and navigate to the directory of the sample.
-
Install the necessary dependencies.
-
Start running the sample.
Here are the commands you can use in your terminal to do this:
git clone -b sample/map-external-layer https://github.com/woosmap/js-samples.git
cd js-samples
npm i
npm start
You can experiment with other samples by switching to any branch that starts with the pattern sample/SAMPLE_NAME
.
git checkout sample/SAMPLE_NAME
npm i
npm start
Was this article helpful?
Have more questions? Submit a request