基于函数结果的Azure条件数据驱动样式



我正在考虑从OpenLayers迁移到Azure Maps,需要查看我目前可以做和需要在Azure上做的事情的列表。其中之一就是造型,

我正在基于一些不同的东西来设置图层的样式,其中之一是如果某个功能的名称存在于数组中。还有一些其他要求,但它们都源于相同的基本需求。

我已经看到,在设置使用样式时,我可以为每个多边形定义一个自定义属性,但我看不出如何基于自定义函数(即,属性是否在数组中(进行设置。

以下是我在OpenLayers(v3(中所做的:

this._style = (feature, resolution) => {

if (elsaMap.titlesWithContactInfo.includes(MapHelpers.Titles.getProperty(feature, MapHelpers.Titles.PROPERTY_NAMES.TITLE_NO))) {
// Client has entered contact info, return named style
return clientAddedContactStyle;
}
// No ownership information
return noOwnershipStyle
}

这可以在Azure地图中完成吗?我已经阅读了关于基于条件表达式的样式的文档,但似乎没有太大帮助。

或者,我可以将层的样式写成纯函数吗?

https://learn.microsoft.com/en-us/azure/azure-maps/data-driven-style-expressions-web-sdk#conditional-表达式

由于样式逻辑被移交给GPU进行处理,因此不支持用于样式的回调函数。但是,您可以将函数逻辑转换为数据驱动的样式表达式。样式表达式可以用于将复杂的函数逻辑重新创建为GPU将处理的一组指令。这比使用回调进行样式设置要快得多,并将此处理从浏览器的单个CPU UI线程卸载到GPU。下面是一个示例表达式,用于检查属性(标题(是否在值数组中,并相应地设置气泡的颜色(相同的主体适用于多边形(。

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
<meta http-equiv="x-ua-compatible" content="IE=Edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<!-- Add references to the Azure Maps Map control JavaScript and CSS files. -->
<link rel="stylesheet" href="https://atlas.microsoft.com/sdk/css/atlas.min.css?api-version=2" type="text/css" />
<script src="https://atlas.microsoft.com/sdk/js/atlas.min.js?api-version=2"></script>
<script type='text/javascript'>
var map, datasource;
function GetMap() {
//Initialize a map instance.
map = new atlas.Map('myMap', {
zoom: 5,
//Add your Azure Maps subscription key to the map SDK. Get an Azure Maps key at https://azure.com/maps
authOptions: {
authType: 'subscriptionKey',
subscriptionKey: 'tTk1JVEaeNvDkxxnxHm9cYaCvqlOq1u-fXTvyXn2XkA'
}
});
//Wait until the map resources are ready.
map.events.add('ready', function () {
//Create a data source to add your data to.
datasource = new atlas.source.DataSource();
map.sources.add(datasource);
//Array of values to look into.
var titlesWithContactInfo = ['titleA', 'titleB', 'titleC'];
//Add some data that has a "title" property.
datasource.add([
new atlas.data.Feature(new atlas.data.Point([0, 0]), { title: 'titleA' }),
new atlas.data.Feature(new atlas.data.Point([0, 1]), { title: 'titleC' }),
new atlas.data.Feature(new atlas.data.Point([1, 1]), { title: 'titleX' })
]);
//Create a layer that styles shapes based on the title property. 
var layer = new atlas.layer.BubbleLayer(datasource, null, {
color: [
'case',
//Get the title property from the feature and see if it is in the list of valid titles. 
['in', ['get', 'title'], ['literal', titlesWithContactInfo]],
//Make the color green if the title is in the array.
'green',
//Make the color red if it isn't.
'red'
]
});
map.layers.add(layer);
});
}
</script>
<style>
html, body {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body onload="GetMap()">
<div id="myMap" style="position:relative;width:100%;height:100%;"></div>
</body>
</html>

请注意,如果使用ContactInfo数组更改title,则需要更新图层中的选项,因为它只知道在设置选项时传入的值。基本上将相同的样式信息传递到层的setOptions函数中。

最新更新