Skip to content
This repository was archived by the owner on Apr 24, 2024. It is now read-only.

Commit a637e5c

Browse files
author
Josh Erb
authored
update default tile/style endpoints for mapbox styles (#1317)
* change base url * add template style mapping to config * point old style templates to equivalent gl styles * update test url expecations * update geocoder test urls * update shareControl iFrame test * update logo mock servers * add console warn util * add classic style warning * fix tile requests when retina = true * account for style-spec default center/zoom * add redirect case and fix map.js tests * add new test cases for default ids * add some additional tilelayer tests
1 parent eda2a24 commit a637e5c

16 files changed

Lines changed: 191 additions & 45 deletions

src/config.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
'use strict';
22

33
module.exports = {
4-
HTTP_URL: 'http://a.tiles.mapbox.com/v4',
5-
HTTPS_URL: 'https://a.tiles.mapbox.com/v4',
4+
HTTP_URL: 'http://api.mapbox.com',
5+
HTTPS_URL: 'https://api.mapbox.com',
66
FORCE_HTTPS: true,
7-
REQUIRE_ACCESS_TOKEN: true
7+
REQUIRE_ACCESS_TOKEN: true,
8+
TEMPLATE_STYLES: {
9+
'mapbox.dark': 'mapbox/dark-v10',
10+
'mapbox.light': 'mapbox/light-v10',
11+
'mapbox.osm-bright': 'mapbox/bright-v9',
12+
'mapbox.outdoors': 'mapbox/outdoors-v11',
13+
'mapbox.satellite': 'mapbox/satellite-v9',
14+
'mapbox.streets': 'mapbox/streets-v11',
15+
'mapbox.streets-basic': 'mapbox/basic-v9',
16+
'mapbox.streets-satellite': 'mapbox/satellite-streets-v11'
17+
}
818
};

src/format_url.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
var config = require('./config'),
4+
warn = require('./util').warn,
45
version = require('../package.json').version;
56

67
module.exports = function(path, accessToken) {
@@ -38,7 +39,14 @@ module.exports.tileJSON = function(urlOrMapID, accessToken) {
3839
if (urlOrMapID.indexOf('/') !== -1)
3940
return urlOrMapID;
4041

41-
var url = module.exports('/v4/' + urlOrMapID + '.json', accessToken);
42+
var url;
43+
if (urlOrMapID in config.TEMPLATE_STYLES) {
44+
url = module.exports('/styles/v1/' + config.TEMPLATE_STYLES[urlOrMapID], accessToken);
45+
} else {
46+
warn('Warning: this implementation is loading a Mapbox Studio Classic style (' + urlOrMapID + '). ' +
47+
'Studio Classic styles are scheduled for deprecation: https://blog.mapbox.com/deprecating-studio-classic-styles-c65a744140a6');
48+
url = module.exports('/v4/' + urlOrMapID + '.json', accessToken);
49+
}
4250

4351
// TileJSON requests need a secure flag appended to their URLs so
4452
// that the server knows to send SSL-ified resource references.

src/load_tilejson.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,18 @@ module.exports = {
88
_loadTileJSON: function(_) {
99
if (typeof _ === 'string') {
1010
_ = format_url.tileJSON(_, this.options && this.options.accessToken);
11+
var isGLStyle = _.indexOf('/styles/v1/') !== -1;
12+
1113
request(_, L.bind(function(err, json) {
1214
if (err) {
1315
util.log('could not load TileJSON at ' + _);
1416
this.fire('error', {error: err});
17+
} else if (json && isGLStyle) {
18+
// In order to preserve compatibility, 256x256 tiles are requested by default
19+
// If you would like better resolution & fewer network requests, use a styleLayer instead
20+
json.tiles = [ format_url('/styles/v1/' + json.owner + '/' + json.id + '/tiles/256/{z}/{x}/{y}', this.options.accessToken) ];
21+
this._setTileJSON(json);
22+
this.fire('ready');
1523
} else if (json) {
1624
this._setTileJSON(json);
1725
this.fire('ready');

src/map.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ var LMap = L.Map.extend({
146146
this._mapboxLogoControl._setTileJSON(json);
147147

148148
if (!this._loaded && json.center) {
149-
var zoom = this.getZoom() !== undefined ? this.getZoom() : json.center[2],
149+
var defaultZoom = (json.zoom) ? json.zoom : json.center[2];
150+
var zoom = this.getZoom() !== undefined ? this.getZoom() : defaultZoom,
150151
center = L.latLng(json.center[1], json.center[0]);
151152

152153
this.setView(center, zoom);

src/tile_layer.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,14 @@ var TileLayer = L.TileLayer.extend({
7979

8080
var templated = L.Util.template(url, tilePoint);
8181
if (!templated || !this.options.format) {
82-
return templated;
82+
var tileURL;
83+
if (L.Browser.retina && url.indexOf('/styles/v1') !== -1) {
84+
tileURL = templated.replace('?', '@2x?');
85+
} else {
86+
tileURL = templated;
87+
}
88+
89+
return tileURL
8390
} else {
8491
return templated.replace(formatPattern,
8592
(L.Browser.retina ? this.scalePrefix : '.') + this.options.format);

src/util.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ function contains(item, list) {
88
return false;
99
}
1010

11+
var warnOnceHistory = {};
12+
1113
module.exports = {
1214
idUrl: function(_, t) {
1315
if (_.indexOf('/') === -1) t.loadID(_);
@@ -19,6 +21,15 @@ module.exports = {
1921
typeof console.error === 'function') {
2022
console.error(_);
2123
}
24+
},
25+
warn: function(_) {
26+
// avoid cluttering the console with duplicative warnings
27+
if (warnOnceHistory[_]) return;
28+
if (typeof console === 'object' &&
29+
typeof console.warn === 'function') {
30+
warnOnceHistory[_] = true;
31+
console.warn(_);
32+
}
2233
/* eslint-enable no-console */
2334
},
2435
strict: function(_, type) {

test/helper.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,31 @@ helpers.tileJSON_noformat = {
933933
"tiles":["http://domain.example/path/{z}/{x}/{y}"]
934934
};
935935

936+
helpers.styleJSON = {
937+
"version": 8,
938+
"name": "Bright",
939+
"tiles": ['https://api.mapbox.com/styles/v1/mapbox/bright-v9/tiles/256/{z}/{x}/{y}?access_token=key'],
940+
"sources": {
941+
"mapbox": {
942+
"url": "mapbox://mapbox.mapbox-streets-v7",
943+
"type": "vector"
944+
}
945+
},
946+
"center": [
947+
-118.2518,
948+
34.0442
949+
],
950+
"zoom": 15,
951+
"sprite": "mapbox://sprites/mapbox/bright-v9",
952+
"glyphs": "mapbox://fonts/mapbox/{fontstack}/{range}.pbf",
953+
"created": "1970-01-01T00:00:00.000Z",
954+
"modified": "1970-01-01T00:00:00.000Z",
955+
"owner": "mapbox",
956+
"id": "bright-v9",
957+
"draft": false,
958+
"visibility": "public"
959+
};
960+
936961
helpers.geoJson = {
937962
type: 'FeatureCollection',
938963
features: [{
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset='UTF-8'/>
5+
<link rel="stylesheet" href="../../dist/mapbox.css"/>
6+
<meta name='viewport' content='initial-scale=1.0 maximum-scale=1.0'>
7+
<link rel="stylesheet" href="embed.css"/>
8+
<script src="../../dist/mapbox.js"></script>
9+
<script src="access_token.js"></script>
10+
</head>
11+
<body>
12+
<div id='map'></div>
13+
<script type='text/javascript'>
14+
L.Browser.retina = true;
15+
L.mapbox.map('map', 'mapbox.outdoors')
16+
.setView([36.1257, -112.0586], 12);
17+
</script>
18+
</body>
19+
</html>

test/spec/format_url.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ describe("format_url", function() {
1010
});
1111

1212
it('returns a v4 URL with access_token parameter', function() {
13-
expect(internals.url('/v4/user.map.json')).to.equal('https://a.tiles.mapbox.com/v4/user.map.json?access_token=key')
13+
expect(internals.url('/v4/user.map.json')).to.equal('https://api.mapbox.com/v4/user.map.json?access_token=key')
1414
});
1515

1616
it('uses provided access token', function() {
17-
expect(internals.url('/v4/user.map.json', 'token')).to.equal('https://a.tiles.mapbox.com/v4/user.map.json?access_token=token')
17+
expect(internals.url('/v4/user.map.json', 'token')).to.equal('https://api.mapbox.com/v4/user.map.json?access_token=token')
1818
});
1919

2020
it('throws an error if no access token is provided', function() {
@@ -39,27 +39,31 @@ describe("format_url", function() {
3939
internals.config.FORCE_HTTPS = true;
4040
internals.config.HTTPS_URL = 'https://api-maps-staging.tilestream.net/v4';
4141
expect(internals.url('/v4/ludacris.map.json')).to.equal('https://api-maps-staging.tilestream.net/v4/ludacris.map.json?access_token=key');
42-
internals.config.HTTPS_URL = 'https://a.tiles.mapbox.com';
42+
internals.config.HTTPS_URL = 'https://api.mapbox.com';
4343
});
4444

4545
describe('.tileJSON', function() {
4646
it('returns the input when passed a URL', function() {
47-
expect(internals.url.tileJSON('http://a.tiles.mapbox.com/v3/user.map.json')).to.equal('http://a.tiles.mapbox.com/v3/user.map.json')
47+
expect(internals.url.tileJSON('http://api.mapbox.com/v3/user.map.json')).to.equal('http://api.mapbox.com/v3/user.map.json')
4848
});
4949

5050
it('returns a v4 URL with access_token parameter, uses https and appends &secure', function() {
51-
expect(internals.url.tileJSON('user.map')).to.equal('https://a.tiles.mapbox.com/v4/user.map.json?access_token=key&secure');
51+
expect(internals.url.tileJSON('user.map')).to.equal('https://api.mapbox.com/v4/user.map.json?access_token=key&secure');
52+
});
53+
54+
it('returns a styles/v1 URL for default mapbox style ids', function() {
55+
expect(internals.url.tileJSON('mapbox.streets')).to.equal('https://api.mapbox.com/styles/v1/mapbox/streets-v11?access_token=key&secure')
5256
});
5357

5458
it('does not append &secure and uses http when FORCE_HTTPS is set to false', function() {
5559
internals.config.FORCE_HTTPS = false;
56-
expect(internals.url.tileJSON('user.map')).to.equal('http://a.tiles.mapbox.com/v4/user.map.json?access_token=key');
60+
expect(internals.url.tileJSON('user.map')).to.equal('http://api.mapbox.com/v4/user.map.json?access_token=key');
5761
});
5862
});
5963

6064
describe('.style', function() {
6165
it('returns a style url with access_token parameter', function() {
62-
expect(internals.url.style('mapbox://styles/bobbysud/cifr15emd00007zlzxjew2rar')).to.equal('https://a.tiles.mapbox.com/styles/v1/bobbysud/cifr15emd00007zlzxjew2rar?access_token=key')
66+
expect(internals.url.style('mapbox://styles/bobbysud/cifr15emd00007zlzxjew2rar')).to.equal('https://api.mapbox.com/styles/v1/bobbysud/cifr15emd00007zlzxjew2rar?access_token=key')
6367
});
6468
});
6569
});

test/spec/geocoder.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,47 +13,47 @@ describe('L.mapbox.geocoder', function() {
1313
it('supports multiple arguments', function() {
1414
var g = L.mapbox.geocoder('mapbox.places');
1515
expect(g.queryURL(['austin', 'houston']))
16-
.to.eql('https://a.tiles.mapbox.com/geocoding/v5/mapbox.places/austin;houston.json?access_token=key');
16+
.to.eql('https://api.mapbox.com/geocoding/v5/mapbox.places/austin;houston.json?access_token=key');
1717
});
1818

1919
it('supports proximity', function() {
2020
var g = L.mapbox.geocoder('mapbox.places');
2121
expect(g.queryURL({query: ['austin', 'houston'], proximity: [10, 15]}))
22-
.to.eql('https://a.tiles.mapbox.com/geocoding/v5/mapbox.places/austin;houston.json?access_token=key&proximity=15,10');
22+
.to.eql('https://api.mapbox.com/geocoding/v5/mapbox.places/austin;houston.json?access_token=key&proximity=15,10');
2323
});
2424

2525
it('supports country option', function() {
2626
var g = L.mapbox.geocoder('mapbox.places');
2727
expect(g.queryURL({query: ['austin', 'houston'], country: 'us'}))
28-
.to.eql('https://a.tiles.mapbox.com/geocoding/v5/mapbox.places/austin;houston.json?access_token=key&country=us');
28+
.to.eql('https://api.mapbox.com/geocoding/v5/mapbox.places/austin;houston.json?access_token=key&country=us');
2929
});
3030

3131
it('supports bbox option', function() {
3232
var g = L.mapbox.geocoder('mapbox.places');
3333
expect(g.queryURL({query: ['austin', 'houston'], bbox: [ -104.0458814, 26.0696823, -93.7347459, 36.4813628 ]}))
34-
.to.eql('https://a.tiles.mapbox.com/geocoding/v5/mapbox.places/austin;houston.json?access_token=key&bbox=-104.0458814,26.0696823,-93.7347459,36.4813628');
34+
.to.eql('https://api.mapbox.com/geocoding/v5/mapbox.places/austin;houston.json?access_token=key&bbox=-104.0458814,26.0696823,-93.7347459,36.4813628');
3535
});
3636

3737
it('supports autocomplete option', function() {
3838
var g = L.mapbox.geocoder('mapbox.places');
3939
expect(g.queryURL({query: ['austin', 'houston'], country: 'us', autocomplete: false}))
40-
.to.eql('https://a.tiles.mapbox.com/geocoding/v5/mapbox.places/austin;houston.json?access_token=key&country=us&autocomplete=false');
40+
.to.eql('https://api.mapbox.com/geocoding/v5/mapbox.places/austin;houston.json?access_token=key&country=us&autocomplete=false');
4141
});
4242

4343
it('rounds proximity params correctly', function() {
4444
var g = L.mapbox.geocoder('mapbox.places');
4545
expect(g.queryURL({query: ['austin', 'houston'], proximity: L.latLng(-10.12345, 15.67890)}))
46-
.to.eql('https://a.tiles.mapbox.com/geocoding/v5/mapbox.places/austin;houston.json?access_token=key&proximity=15.679,-10.123');
46+
.to.eql('https://api.mapbox.com/geocoding/v5/mapbox.places/austin;houston.json?access_token=key&proximity=15.679,-10.123');
4747
});
4848

4949
it('rounds reverse ')
5050

5151
it('supports types', function() {
5252
var g = L.mapbox.geocoder('mapbox.places');
5353
expect(g.queryURL({query: ['austin', 'houston'], types: 'place'}))
54-
.to.eql('https://a.tiles.mapbox.com/geocoding/v5/mapbox.places/austin;houston.json?access_token=key&types=place');
54+
.to.eql('https://api.mapbox.com/geocoding/v5/mapbox.places/austin;houston.json?access_token=key&types=place');
5555
expect(g.queryURL({query: ['austin', 'houston'], types: ['place', 'address']}))
56-
.to.eql('https://a.tiles.mapbox.com/geocoding/v5/mapbox.places/austin;houston.json?access_token=key&types=place,address');
56+
.to.eql('https://api.mapbox.com/geocoding/v5/mapbox.places/austin;houston.json?access_token=key&types=place,address');
5757
});
5858
});
5959

@@ -62,7 +62,7 @@ describe('L.mapbox.geocoder', function() {
6262
var g = L.mapbox.geocoder('mapbox.places');
6363

6464
server.respondWith('GET',
65-
'https://a.tiles.mapbox.com/geocoding/v5/mapbox.places/austin;houston.json?access_token=key',
65+
'https://api.mapbox.com/geocoding/v5/mapbox.places/austin;houston.json?access_token=key',
6666
[200, { 'Content-Type': 'application/json' }, JSON.stringify(helpers.geocoderBulk)]);
6767

6868
g.query(['austin', 'houston'], function(err, res) {
@@ -77,7 +77,7 @@ describe('L.mapbox.geocoder', function() {
7777
var g = L.mapbox.geocoder('mapbox.places');
7878

7979
server.respondWith('GET',
80-
'https://a.tiles.mapbox.com/geocoding/v5/mapbox.places/austin.json?access_token=key',
80+
'https://api.mapbox.com/geocoding/v5/mapbox.places/austin.json?access_token=key',
8181
[200, { "Content-Type": "application/json" }, JSON.stringify(helpers.geocoderAustin)]);
8282

8383
g.query('austin', function(err, res) {
@@ -93,7 +93,7 @@ describe('L.mapbox.geocoder', function() {
9393
var g = L.mapbox.geocoder('mapbox.places');
9494

9595
server.respondWith('GET',
96-
'https://a.tiles.mapbox.com/geocoding/v5/mapbox.places/nonesuch.json?access_token=key',
96+
'https://api.mapbox.com/geocoding/v5/mapbox.places/nonesuch.json?access_token=key',
9797
[200, { 'Content-Type': 'application/json' }, JSON.stringify({"type":"FeatureCollection","query":["nonesuch"],"features":[]})]);
9898

9999
g.query('nonesuch', function(err, res) {
@@ -110,7 +110,7 @@ describe('L.mapbox.geocoder', function() {
110110
var g = L.mapbox.geocoder('mapbox.places');
111111

112112
server.respondWith('GET',
113-
'https://a.tiles.mapbox.com/geocoding/v5/mapbox.places/-97.7%2C30.3.json?access_token=key',
113+
'https://api.mapbox.com/geocoding/v5/mapbox.places/-97.7%2C30.3.json?access_token=key',
114114
[200, { "Content-Type": "application/json" }, JSON.stringify(helpers.geocoderReverse)]);
115115

116116
g.reverseQuery({ lat: 30.3, lng: -97.7 }, function(err, res) {
@@ -125,7 +125,7 @@ describe('L.mapbox.geocoder', function() {
125125
var g = L.mapbox.geocoder('mapbox.places');
126126

127127
server.respondWith('GET',
128-
/https:\/\/a\.tiles\.mapbox\.com\/geocoding\/v5\/mapbox.places\/[\-\d\.]+(%2C|,)[\-\d\.]+\.json\?access_token=key/,
128+
/https:\/\/api\.mapbox\.com\/geocoding\/v5\/mapbox.places\/[\-\d\.]+(%2C|,)[\-\d\.]+\.json\?access_token=key/,
129129
[200, { "Content-Type": "application/json" }, JSON.stringify(helpers.geocoderReverseRounded)]);
130130

131131
g.reverseQuery({ lat: 30.1234567890, lng: -97.0987654321 }, function(err, res) {

0 commit comments

Comments
 (0)