Background
I am playing around with mapbox and uploaded a shapefile from which I created a vector tileset. I loaded the tileset into mapxbox studio to create a custom style. My aim is to solely display the vector tileset with no background layer at all. I am doing this for the reason because I want to overlay the tileset over another raster tileset via leaflet later on. In mapbox studio this looks as follows - you can see the colored vectors and the surrounding transparent area:
Problem:
When I try to visualize both tilesets via leaflet, I am not able to see through the first tileset. Instead I am seeing my colored vector with a black background:
Any chance I can make the tileset background transparent? So the vectors wont be surrounded by a black background anymore but rather by the underlying raster tile set?
Here some code snippets I am using:
<div id='map'>
</div>
<script>
var sat = L.tileLayer('https://api.mapbox.com/styles/v1/xxx/xxx/tiles/256/{z}/{x}/{y}?access_token=xxx', {
attribution: 'Raster tile set'
});
var clc = L.tileLayer('https://api.mapbox.com/styles/v1/xxx/xxx/tiles/256/{z}/{x}/{y}?access_token=xxx', {
attribution: 'Transparent vector tile set'
});
var map = L.map('map', {
center: [48.2, 16.37],
zoom: 10,
layers: [sat, clc]
});
</script>
Related
I am using mapbox gl javascript. I have a large geojson data which contains point location (Latitude, Longitude) pair along with some properties. I want to load the large geojson into mapbox studio, and convert it into vector tiles.
Next important task for me is to load the vector tileset using mapbox gl javascript and show custom marker icon for the points in vector tiles. Can you please suggest how can I do that ?
The custom markers setting are explained here : https://docs.mapbox.com/mapbox-gl-js/example/custom-marker-icons/
There are differents ways to display your tileset with its markers. Could you be more precise on what you are trying to do ? Do you want to display them from a web page ?
Meanwhile, you can try to upload you GeoJSON in a new tileset in your Mapbox Studio account. Then create a new style > new layer > select data from your tileset.
At this point, you put your data on Symbol type, then go to style > icon and select your custom icon you previously upload from the Images button at the right top of the editor.
Bon courage,
N.
Please have a look at the answer on my code block to load custom image for a layer.
Note:
I am using vector data source.
Source data consists of points (Lat Lng pair).
Attached image is for reference.
enter image description here
map.addSource('ccc_location', {
type: 'vector',
url: 'mapbox://aciapprover111.4jkdyq5t'
});
map.loadImage('#Url.Content("~/Content/assets/img/green.png")', function (error, green) { //this is where we load the image file
if (error) throw error;
map.addImage("custom-green", green); //this is where we name the image file we are loading
map.addLayer({
'id': 'CCCLocations_VGO',
'type': 'symbol',
'source': 'ccc_location',
"filter": ["all", ["==", "type", "VGO"]],
'source-layer': 'CCCLocations-bgcof4',
'layout': {
'icon-image': "custom-green",
'icon-size': 0.65,
'icon-allow-overlap': true
}
});
});
A good place to start is working through some of the Mapbox-GL-JS examples, and reading the documentation.
I have created some custom styles in mapbox studio. They are essentially just the mapbox outdoors style and the mabpox satellite style, each customized, separated out into 2 styles each: the basemap with no labels at all, and only the labels without the basemap. I want to give the user the ability to toggle layers on and off, or set the transparency. You can see the layers here:
Outdoors labels only
Satellite labels only
Within mapbox studio, the tiles show transparency where there are no roads / labels / etc. This is what I would expect. I am then using these layers in leaflet like so:
var mapBoxOutdoorsLabels = L.tileLayer(
'https://api.mapbox.com/styles/v1/slutske22/ck87tp2fq0rq41iqox69g4ko5/tiles/256/{z}/{x}/{y}#2x?access_token={accessToken}',
{ accessToken ,maxZoom: 18, pane: 'labels'})
.addTo(map1)
var mapBoxSatelliteLabels = L.tileLayer(
'https://api.mapbox.com/styles/v1/slutske22/ck8i7fv4h0h771ipc6mwzwmp4/tiles/256/{z}/{x}/{y}#2x?access_token={accessToken}',
{ accessToken ,maxZoom: 18, pane: 'labels'})
.addTo(map2)
As far as I can tell, the way I'm importing these two layers is identical. But for some reason, my labels layer for outdoors shows up properly (with transparent background), while my labels layer for satellite shows up with all black background, and you cannot see through to the basemap. The map on the right is the problem:
Here is a working codesandbox of the problem
I'm not sure what I'm doing wrong in mapbox studio or in my leaflet import for the tiles to be generated with black instead of transparent. The way I'm building the two maps are identical, at least as far as I can tell. Any ideas?
Thanks for reading.
The background layers in your labels mapbox style are not transparent.
Here is a codepen with a copy of your style with the background fixed
What I did:
Removing the /tiles/{x}/{y}/{z}/ part from your style's URL and running that in a browser returns the json from mapbox.
There I saw that your background layer color is missing the alpha channel value.
"layers": [
{
"id": "background",
"type": "background",
"layout": {
"visibility": "none"
},
"paint": {
"background-color": "hsl(222, 56%, 4%)" <= SHOULD BE (222, 56%, 4%, 0)
},
I was able to save the json file and upload it as my own style on https://studio.mapbox.com/
there I changed the alpha channel of the background and reduced the opacity of the satellite layer.
It appears that "visibility: none" doesn't work as intended
exported as a new style, I updated the fork with my style and access token
My code like this:
var source = new ol.source.VectorTile({
format:new ol.format.MVT(),
tileGrid:ol.tilegrid.createXYZ({maxZoom:22}),
tilePixelRatio:1,
url:'http://127.0.0.1:8080/xxx.pbf'
});
But on the edge of tiles, marker symbol can not show point intact, it is cut by edges.
Is there any solution for this? thanks.
My reading of the Leaflet and Mapzen documentations suggest that in order to use a custom tile provider with Leaflet, one needs to do just two things:
Specify the tile provider in L.tilelayer(urlTemplateToTileProvider)
Set this provider to MapZen
var urlTemplateToTileProvider =
'http://tile.mapzen.com/mapzen/vector/v1/all/{z}/{x}/{y}.mvt?api_key=apiKey'
However, when I try this I end up with an empty map which then proceeds to display markers etc correctly. And yet a manual test for a generated tile URL, e.g.
http://tile.mapzen.com/mapzen/vector/v1/all/14/8471/5583.mvt?api_key=apiKey
did in fact download some - unintelligible to me - data.
I also tried using the other two formats mentioned in the Mapzen docs (.json and .geojson) but with exactly the same result. Given that the latter two formats return human readable data, I checked them out for my test tile in my browser and the data is indeed for the area I want to use.
Curiously enough, the Leaflet docs and tutorials request a PNG tile layer (http://{s}.tile.osm.org/{z}/{x}/{y}.png), not raw data.
What am I doing wrong here?
The Tile Layer is for raster tiles (i.e. plain images, like the PNG format for example).
Mapzen delivers vector tiles. To use them with Leaflet, you could use a plugin, e.g. Leaflet.VectorGrid (license)
Display gridded vector data (sliced GeoJSON or protobuf vector tiles) in Leaflet 1.0.0
See the demo, which includes tiles from Mapzen
var mapzenTilesUrl = "https://tile.mapzen.com/mapzen/vector/v1/all/{z}/{x}/{y}.mvt?api_key={apikey}";
var mapzenVectorTileOptions = {
rendererFactory: L.canvas.tile,
attribution: '© MapZen, © OpenStreetMap contributors',
vectorTileLayerStyles: vectorTileStyling,
apikey: 'KEY',
};
var mapzenTilesPbfLayer = L.vectorGrid.protobuf(mapzenTilesUrl, mapzenVectorTileOptions);
Since you have raw data in the vector tiles, you need to provide the styling spec (vectorTileStyling)
How can I deal with overlapping lines in the Leaflet map library?
I download geoJSON from the server sid and draw it right onto the map. If there are two identical entries, Leaflet draws them twice. This could be fixed by finding exact duplicated on the server side.
That however doesn't work for different datasets sharing some of the same space. As a result parts of both are drawn onto the same spot.
It appears that the lines are being rendered with the default Leaflet Polyline opacity of 0.5. If you were instantiating the Polylines yourself in code, you could override the opacity to make the lines opaque in this manner:
var myPolyLine = new L.Polyline( myPoints, { opacity: 1 } );
The line that would appear on top would then be the line that you added to the map last (one or the other is going to be on top, unless you make them both opaque and the same color). But this may be moot if you are loading in geoJSON directly and don't have control over how Leaflet renders it.