Skip to content

Instantly share code, notes, and snippets.

@danswick
Last active April 15, 2019 02:56
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danswick/a4de5c170c04fdd38bb4 to your computer and use it in GitHub Desktop.
Save danswick/a4de5c170c04fdd38bb4 to your computer and use it in GitHub Desktop.
3rd party raster tiles in Mapbox GL JS

Simple example demonstrating how to use third-party raster tiles in Mapbox GL JS.

<html>
<head>
<meta charset='utf-8' />
<title></title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.11.4/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.11.4/mapbox-gl.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<div id='map'></div>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoiZGFuc3dpY2siLCJhIjoieUZiWmwtVSJ9.0cPQywdbPVmvHiHJ6NwdXA';
var map = new mapboxgl.Map({
container: 'map', // container id
style: {
"version": 8,
"sources": {
"simple-tiles": {
"type": "raster",
// point to our third-party tiles. Note that some examples
// show a "url" property. This only applies to tilesets with
// corresponding TileJSON (such as mapbox tiles).
"tiles": [
"http://a.tile.openstreetmap.org/{z}/{x}/{y}.png",
"http://b.tile.openstreetmap.org/{z}/{x}/{y}.png"
],
"tileSize": 256
}
},
"layers": [{
"id": "simple-tiles",
"type": "raster",
"source": "simple-tiles",
"minzoom": 0,
"maxzoom": 22
}]
},
center: [-74.50, 40], // starting position
zoom: 2 // starting zoom
});
map.addControl(new mapboxgl.Navigation());
</script>
</body>
</html>
@AdamHeidfeld
Copy link

Hi,
I am working on mapboxgl lately. Your GitHub repo help me a lot. Thanks.

I am trying to do pretty much the same thing as you did in this html. Showing third part raster tile as base map in mapbox-gl. Problem is I failed. It says "Failed to load resource: Cross-origin redirection to https://a.tile.openstreetmap.org/3/7/1.png denied by Cross-Origin Resource Sharing policy: Origin null is not allowed by Access-Control-Allow-Origin.", like you can see in the attachment. Seems openstreetmap don't allow CORS calls.

So my question is, is this piece of code still working for you or not. I want to find some clue to identify the problem I am facing. Thanks a lot.

image

@wysisoft
Copy link

I think that error is a false positive, I think you actually get another error and chrome says that error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment