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

标签 javascript azure geospatial openlayers-3 azure-maps

我正在考虑从 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 Maps 中完成吗?我已经阅读了有关基于条件表达式的样式的文档,但它似乎没有太大帮助。

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

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

最佳答案

不支持样式回调函数,因为样式逻辑将交给 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>

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

关于javascript - 基于函数结果的 Azure 条件数据驱动样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63231698/

相关文章:

Azure DevOps 在给定现有 YAML 的情况下通过 REST API 创建构建定义

java - 原始地理坐标和图形节点之间的最短路径

javascript - 使 Promise.race 仅在成功时完成

javascript - webkit-filter 后获取元素背景色

javascript - 如何防止 <img> 标签加载其图像?

javascript - 需要了解如何实现它

azure - Hyper-v 管理器在 Windows Azure 门户的 RDP 中不可用

azure - 当设备在未在 IoT 中心注册的情况下发送遥测消息时,IoT 中心会发生什么行为?

ruby - RGeo交点函数的问题

mysql - C# mysql 75英里半径 map 覆盖整个美国