Is it possible to utilize the built-in properties
attribute of a GeoJSON data source to automatically style a Mapbox GL map without having to pre-specify a style?
I have a use case where I dynamically load in GeoJSON LineString
data that needs to be a particular color and it already has style data built in:
{"properties": {"color": "#ffff33"...
Right now, I have a rather inelegant setup where the relevant color is parsed out, translated into a style, and added to the map upon every load:
$scope.patternPropToStyle = function(props) {
var id = 'pattern' + props.pid;
var style = {
"id": id,
"type": "line",
"source": id,
"render": {
"$type": "LineString",
"line-join": "round",
"line-cap": "round"
},
"paint": {
"line-color": props.color,
"line-width": 8
}
}
$scope._mapStyle.layers.push(style);
$scope._map.setStyle($scope._mapStyle);
}
I feel like there has to be a better way to do this.
Currently, no - simplestyle-spec isn't supported in Mapbox GL. Currently the GeoJSON support is still very primitive, given that the focus was initially on vector tiles. simplestyle-spec support is certainly in the plans.
来源:https://stackoverflow.com/questions/28470476/do-embedded-geojson-styles-work-with-mapbox-gl