-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Map#loadImage
method and Map#addImage
examples
#4478
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
--- | ||
layout: example | ||
category: example | ||
title: Add a generated icon to the map | ||
description: Add an icon to the map that was generated at runtime. | ||
tags: | ||
- styles | ||
- layers | ||
--- | ||
<div id='map'></div> | ||
|
||
<script> | ||
|
||
var map = new mapboxgl.Map({ | ||
container: 'map', | ||
style: 'mapbox://styles/mapbox/streets-v9' | ||
}); | ||
|
||
map.on('load', () => { | ||
|
||
const width = 64; // The image will be 64 pixels square | ||
const bytesPerPixel = 4; // Each pixel is represented by 4 bytes: red, green, blue, and alpha. | ||
const image = new Uint8Array(width * width * bytesPerPixel); | ||
|
||
for (var x = 0; x < width; x++) { | ||
for (var y = 0; y < width; y++) { | ||
const offset = (y * width + x) * bytesPerPixel; | ||
image[offset + 0] = y / width * 255; // red | ||
image[offset + 1] = x / width * 255; // green | ||
image[offset + 2] = 128; // blue | ||
image[offset + 3] = 255; // alpha | ||
} | ||
} | ||
|
||
map.addImage('gradient', image, {width: width, height: width}); | ||
|
||
map.addLayer({ | ||
"id": "points", | ||
"type": "symbol", | ||
"source": { | ||
"type": "geojson", | ||
"data": { | ||
"type": "FeatureCollection", | ||
"features": [{ | ||
"type": "Feature", | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [0, 0] | ||
} | ||
}] | ||
} | ||
}, | ||
"layout": { | ||
"icon-image": "gradient" | ||
} | ||
}); | ||
}); | ||
|
||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
--- | ||
layout: example | ||
category: example | ||
title: Add an icon to the map | ||
description: Add an icon to the map from an external URL and use it in a symbol layer. | ||
tags: | ||
- styles | ||
- layers | ||
--- | ||
<div id='map'></div> | ||
|
||
<script> | ||
|
||
var map = new mapboxgl.Map({ | ||
container: 'map', | ||
style: 'mapbox://styles/mapbox/streets-v9' | ||
}); | ||
|
||
map.on('load', () => { | ||
map.loadImage('https://upload.wikimedia.org/wikipedia/commons/thumb/6/60/Cat_silhouette.svg/400px-Cat_silhouette.svg.png', (error, image) => { | ||
if (error) throw error; | ||
map.addImage('cat', image); | ||
map.addLayer({ | ||
"id": "points", | ||
"type": "symbol", | ||
"source": { | ||
"type": "geojson", | ||
"data": { | ||
"type": "FeatureCollection", | ||
"features": [{ | ||
"type": "Feature", | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [0, 0] | ||
} | ||
}] | ||
} | ||
}, | ||
"layout": { | ||
"icon-image": "cat", | ||
"icon-size": 0.25 | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you remember why we have
es6
env set tofalse
a few lines below? As far as I remember, turning this back totrue
would allow not specifying common globals like this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As best I remember we were just to avoid using es6 features with limited browser support. This is less of a concern now. I have enabled the "es6" flag and removed this global in the latest commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to continue to write examples without using ES6 features such as
let
/const
or arrow functions (not supported by iOS 9) andfor...of
(not supported by IE 11). I think it's useful to keep this configuration option to catch accidental uses of these types of features. I suggest we revert the latest commit.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right -- the examples don't get
bubleify
ied. I'll revert the last commit and leave a comment about this in the eslint file.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jfirebaugh as far as I remember, the
env
switches are only for predefined globals likeUint8Array
— they're not used for catching keywords likelet
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, you're right. And
"parserOptions": { "ecmaVersion": 5 }
doesn't work either. 😞