Can Azure Maps keep the display of linestring shape for all zoom Levels?

84 views Asked by At

I would like to know if it is possible for Azure Maps to keep the LineStrings for all zoom levels.

Here an exemple : I have a line which will be invisible at zoom level <~ 12 and reappear at zoom level >~ 12

enter image description here

enter image description here

enter image description here

My source code :

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Animate a snakeline - Azure Maps Web SDK Samples</title>
    <meta charset="utf-8" />
    
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <meta name="author" content="Microsoft Azure Maps" /><meta name="version" content="1.0" />
    <meta name="screenshot" content="screenshot.gif" />
    <!-- Add references to the Azure Maps Map control JavaScript and CSS files. -->
    <link href="https://atlas.microsoft.com/sdk/javascript/mapcontrol/3/atlas.min.css" rel="stylesheet" />
    <script src="https://atlas.microsoft.com/sdk/javascript/mapcontrol/3/atlas.min.js"></script>
    <script>
        var map, line;
        //Create an array of points to define a path to animate along.
        var path = [
            [7.4204901, 43.7369253],
            [7.420396249439936, 43.73687484884258]
        ];
        function GetMap() {
            //Initialize a map instance.
            map = new atlas.Map('myMap', {
                center: [7.42, 43.73],
                zoom: 14,
                view: 'Auto',
                //Add authentication details for connecting to Azure Maps.
                authOptions: {
                    authType: '',
                    subscriptionKey: ''
                }
            });
            //Wait until the map resources are ready.
            map.events.add('ready', function () {
                line = new atlas.Shape(new atlas.data.LineString(path));
                //Create a data source and add it to the map.
                datasource = new atlas.source.DataSource();
                map.sources.add(datasource);
                //Add the data to the data source.
                datasource.add(line);
                //Add a layer for rendering line data. 
                map.layers.add(new atlas.layer.LineLayer(datasource, null, {
                    strokeColor: 'blue',
                    strokeWidth: 5,
                }));
                
                
                map.events.add('zoomend', () => console.log(map.getCamera().zoom));
            });
        }
    </script>
</head>
<body onload="GetMap()">
    <div id="myMap" style="position:relative;width:100%;min-width:290px;height:600px;"></div>
</body>
</html>
2

There are 2 answers

2
rbrundritt On

The short answer is no. In your scenario the linestring is small and when zoomed out <12, the linestring is less than 1 pixel in size and thus not rendered.

That said, in scenarios like this where you still want to see where the line is, a common technique is to represent the line in another way when zoomed out. A simple option is to use a bubble layer. This requires storing a single point for each line so that the render doesn't skip the lines point based on its length. This could be put into the same data source you use for your line, or added to a separate data source. Either way you will likely need to a little extra code to make sure the point is removed/edited if the line is removed/edited, assuming you plan on changing the data as during the user experience. If you are only going to load the data once and not make any changes, then that sync logic isn't needed. Here is the code for when you don't need to do any syncing of the data.

var pointSource = new atlas.source.DataSource();
map.sources.add(pointSource);

//Create a Point object from the first position of the line.
pointSource.add(new atlas.Shape(new atlas.data.Point(path[0])));

//Add a layer for rendering line data. 
map.layers.add([
    new atlas.layer.LineLayer(datasource, null, {
        strokeColor: 'blue',
        strokeWidth: 5,
    }),

    new atlas.layer.BubbleLayer(pointSource, null, {
        color: 'blue', //Make the bubble the same color as your line. 
        radius: 2.5, //Make the radius half the lines stroke width, so that it appears as the same size.
        strokeWidth: 0 //Don't outline the bubble.
    })
]);

If you want to add support for syncing the point as the line data changes, here is some helper methods for this:

function addLine(path) {
    var line = new atlas.Shape(new atlas.data.LineString(path));
    
    //Add the line to the data source.
    datasource.add(line);
    
    //Create a Point object from the first position of the line.
    var point = new atlas.Shape(new atlas.data.Point(path[0]));
    
    //Add point to a data source.
    pointSource.add(point);
    
    //Set the point ID as a property on the line.
    line.setProperties({ 
        pointID: point.getId()  
    })
}

//If using the drawing tools to edit the line, the event handlers provide you with the line shape which you could pass into here to sync. 
function lineChanged(line) {
    //Get the point by ID.
    var point = pointSource.getShapeById(line.getProperties().pointID);
    
    //Update the point to the first position of the line as it might have changed locations.
    point.setCoordinates(line.getCoordinates()[0]);
}

function removeLine(line) {     
    datasource.remove(line);
    
    //Get the point by ID, then remove it from the data source.
    pointSource.remove(pointSource.getShapeById(line.getProperties().pointID));
}

To get a little more advanced, you could limit the bubble layer so that it only renders when the line doesn't. I would only do this if you have a lot of lines (tens of thousands) and to optimize performance. If all your lines are around the same size you could just set this to display when zoom is less than 12. If you have lines of difference lengths, you could instead calculate the length, and then determine which zoom level the line should appear at, then store that as a property of the point, and use the filter option of the bubble layer to only render the points for lines that are likely not displayed. Here is a further extension of the code above that adds this logic:

Modify the addLine function to capture the line length information, determine the min zoom level, and add to the point properties.

function addLine(path) {
    var line = new atlas.Shape(new atlas.data.LineString(path));
    
    //Add the line to the data source.
    datasource.add(line);
    
    //Create a Point object from the first position of the line.
    var point = new atlas.Shape(new atlas.data.Point(path[0]));
    
    //Add point to a data source.
    pointSource.add(point);
    
    //Set the point ID as a property on the line.
    line.setProperties({ 
        pointID: point.getId()  
    })
    
    //Calculate line length, determine minimum zoom level, add as property of the point.
    var length = atlas.math.getLengthOfPath(line.getCoordinates()); //Default distance unit is meters.
    
    //Determine the  https://learn.microsoft.com/en-us/azure/azure-maps/zoom-levels-and-tile-grid?tabs=typescript
    var zoom = 0;
    if(length < 156543) zoom = 1;
    else if(length < 78271.5) zoom = 2;
    else if(length < 39135.8) zoom = 3;
    else if(length < 19567.88) zoom = 4;
    else if(length < 9783.94) zoom = 5;
    else if(length < 4891.97) zoom = 6;
    else if(length < 2445.98) zoom = 7;
    else if(length < 1222.99) zoom = 8;
    else if(length < 611.5) zoom = 9;
    else if(length < 305.75) zoom = 10;
    else if(length < 152.87) zoom = 11;
    else if(length < 76.44) zoom = 12;
    else if(length < 38.219) zoom = 13;
    else if(length < 19.109) zoom = 14;
    else if(length < 9.555) zoom = 15;
    else if(length < 4.777) zoom = 16;
    else if(length < 2.3887) zoom = 17;
    else if(length < 1.1943) zoom = 18;
    else if(length < 0.5972) zoom = 19;
    else if(length < 0.2986) zoom = 20;
    else if(length < 0.14929) zoom = 21;
    else if(length < 0.074646) zoom = 22;
    else if(length < 0.037323) zoom = 23;
    else if(length < 0.0186615) zoom = 24;
    
    //Store zoom as proeprty of the point.
    point.setProperties({ 
        lineZoom: zoom
    });
}

//From there you can update the bubble layer to filter the rendered points based on the maps zoom level.

new atlas.layer.BubbleLayer(pointSource, null, {
    color: 'blue', //Make the bubble the same color as your line. 
    radius: 2.5, //Make the radius half the lines stroke width, so that it appears as the same size.
    strokeWidth: 0, //Don't outline the bubble.
    filter: ['<', ['get', 'lineZoom'],['zoom']]
})

Another approach to render the data on a server as a tile layer where you can customize how the data is rendered in this scenario, but that is likely overkill.

0
HGaaloul On

Thank you for your answer

I understood well

in fact, our need is to display multiple linestrings which are linked, is there a way to keep them visible for all zoom levels

I modified my example with a real example to better understand our need

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Animate a snakeline - Azure Maps Web SDK Samples</title>

    <meta charset="utf-8" />
    
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <meta name="author" content="Microsoft Azure Maps" /><meta name="version" content="1.0" />
    <meta name="screenshot" content="screenshot.gif" />

    <!-- Add references to the Azure Maps Map control JavaScript and CSS files. -->
    <link href="https://atlas.microsoft.com/sdk/javascript/mapcontrol/3/atlas.min.css" rel="stylesheet" />
    <script src="https://atlas.microsoft.com/sdk/javascript/mapcontrol/3/atlas.min.js"></script>

    <script>
        var map, line;
                
        var sections = [
        [
            [                7.4204901,                43.7369253            ],
            [                7.420396249439936,                43.73687484884258            ]
        ],
        [
            [                7.419741388919931,                43.73634123464023            ],
            [                7.419664477004235,                43.73626803889507            ]
        ],
        [
            [
                7.419664477004235,
                43.73626803889507
            ],
            [
                7.419587433439927,
                43.73619471786219
            ]
        ],
        [
            [
                7.419587433439927,
                43.73619471786219
            ],
            [
                7.4195333,
                43.7361432
            ],
            [
                7.419509785920206,
                43.73612175402602
            ]
        ],
        [
            [
                7.419509785920206,
                43.73612175402602
            ],
            [
                7.4194503,
                43.7360675
            ],
            [
                7.419431589554446,
                43.73604907021113
            ]
        ],
        [
            [
                7.419431589554446,
                43.73604907021113
            ],
            [
                7.4193903,
                43.7360084
            ],
            [
                7.419354310692053,
                43.73597497020918
            ]
        ],
        [
            [
                7.419354310692053,
                43.73597497020918
            ],
            [
                7.419275524977548,
                43.73590178764177
            ]
        ],
        [
            [
                7.43857519967934,
                43.7498657586202
            ],
            [
                7.438495633322829,
                43.74979661773166
            ]
        ],
        [
            [
                7.438495633322829,
                43.74979661773166
            ],
            [
                7.4384108,
                43.7497229
            ]
        ],
        [
            [
                7.4400893,
                43.7508798
            ],
            [
                7.4400375,
                43.7508544
            ],
            [
                7.4400001,
                43.7508228
            ],
            [
                7.4399593,
                43.7507822
            ]
        ],
        [
            [
                7.4400937,
                43.7507909
            ],
            [
                7.4401756,
                43.7507761
            ]
        ],
        [
            [
                7.4237927,
                43.7375564
            ],
            [
                7.423904725874978,
                43.73754802947848
            ]
        ],
        [
            [
                7.424794733990175,
                43.73748152848405
            ],
            [
                7.42490514539656,
                43.73747327859487
            ]
        ],
        [
            [
                7.42490514539656,
                43.73747327859487
            ],
            [
                7.425015346176639,
                43.73746504444359
            ]
        ],
        [
            [
                7.425015346176639,
                43.73746504444359
            ],
            [
                7.42512533117111,
                43.73745682641571
            ]
        ],
        [
            [
                7.42512533117111,
                43.73745682641571
            ],
            [
                7.42521,
                43.7374505
            ],
            [
                7.425235125061128,
                43.73744904295968
            ]
        ],
        [
            [
                7.425235125061128,
                43.73744904295968
            ],
            [
                7.425344790184752,
                43.73744268331319
            ]
        ],
        [
            [
                7.425344790184752,
                43.73744268331319
            ],
            [
                7.4254221,
                43.7374382
            ],
            [
                7.425454271830416,
                43.73743748876951
            ]
        ],
        [
            [
                7.425454271830416,
                43.73743748876951
            ],
            [
                7.425563614712192,
                43.73743507149978
            ]
        ],
        [
            [
                7.425563614712192,
                43.73743507149978
            ],
            [
                7.4256347,
                43.7374335
            ],
            [
                7.425672722596617,
                43.73743396478022
            ]
        ],
        [
            [
                7.425672722596617,
                43.73743396478022
            ],
            [
                7.425781579587848,
                43.73743529542514
            ]
        ],
        [
            [
                7.425781579587848,
                43.73743529542514
            ],
            [
                7.4258474,
                43.7374361
            ],
            [
                7.425890123499366,
                43.73743815265988
            ]
        ],
        [
            [
                7.423904725874978,
                43.73754802947848
            ],
            [
                7.424016590470744,
                43.73753967100765
            ]
        ],
        [
            [
                7.425890123499366,
                43.73743815265988
            ],
            [
                7.425998309498395,
                43.73744335047991
            ]
        ],
        [
            [
                7.425998309498395,
                43.73744335047991
            ],
            [
                7.4260597,
                43.7374463
            ],
            [
                7.426106101115234,
                43.73745014115192
            ]
        ],
        [
            [
                7.426106101115234,
                43.73745014115192
            ],
            [
                7.426213465365034,
                43.73745902892095
            ]
        ],
        [
            [
                7.426213465365034,
                43.73745902892095
            ],
            [
                7.4262711,
                43.7374638
            ],
            [
                7.426320346348565,
                43.7374696392099
            ]
        ],
        [
            [
                7.426320346348565,
                43.7374696392099
            ],
            [
                7.426426718222041,
                43.7374822518749
            ]
        ],
        [
            [
                7.426426718222041,
                43.7374822518749
            ],
            [
                7.4264811,
                43.7374887
            ],
            [
                7.426532770977397,
                43.73749493842162
            ]
        ],
        [
            [
                7.426532770977397,
                43.73749493842162
            ],
            [
                7.426638484083929,
                43.73750770154248
            ]
        ],
        [
            [
                7.426638484083929,
                43.73750770154248
            ],
            [
                7.4266774,
                43.7375124
            ],
            [
                7.426743419642921,
                43.73752339193024
            ]
        ],
        [
            [
                7.426743419642921,
                43.73752339193024
            ],
            [
                7.426847762605043,
                43.73754076449558
            ]
        ],
        [
            [
                7.426847762605043,
                43.73754076449558
            ],
            [
                7.4268714,
                43.7375447
            ],
            [
                7.426951089846081,
                43.73756168103003
            ]
        ],
        [
            [
                7.424016590470744,
                43.73753967100765
            ],
            [
                7.424128291734569,
                43.73753132474092
            ]
        ],
        [
            [
                7.426951089846081,
                43.73756168103003
            ],
            [
                7.427053861970331,
                43.73758358063975
            ]
        ],
        [
            [
                7.427053861970331,
                43.73758358063975
            ],
            [
                7.4270624,
                43.7375854
            ],
            [
                7.427155286513694,
                43.73760960107778
            ]
        ],
        [
            [
                7.427155286513694,
                43.73760960107778
            ],
            [
                7.4272497,
                43.7376342
            ],
            [
                7.427256236902215,
                43.73763600639785
            ]
        ],
        [
            [
                7.427256236902215,
                43.73763600639785
            ],
            [
                7.4273076,
                43.7376502
            ],
            [
                7.427355498953117,
                43.73766675686745
            ]
        ],
        [
            [
                7.427355498953117,
                43.73766675686745
            ],
            [
                7.427453413591314,
                43.73770060227702
            ]
        ],
        [
            [
                7.427453413591314,
                43.73770060227702
            ],
            [
                7.4274563,
                43.7377016
            ],
            [
                7.427550328280496,
                43.73773617622213
            ]
        ],
        [
            [
                7.427550328280496,
                43.73773617622213
            ],
            [
                7.4276143,
                43.7377597
            ],
            [
                7.427646267856637,
                43.73777320195187
            ]
        ],
        [
            [
                7.427646267856637,
                43.73777320195187
            ],
            [
                7.42774067940124,
                43.7378130776353
            ]
        ],
        [
            [
                7.42774067940124,
                43.7378130776353
            ],
            [
                7.4277914,
                43.7378345
            ],
            [
                7.427834626874414,
                43.73785313525632
            ]
        ],
        [
            [
                7.427834626874414,
                43.73785313525632
            ],
            [
                7.42792806024,
                43.73789341470609
            ]
        ],
        [
            [
                7.424128291734569,
                43.73753132474092
            ],
            [
                7.424239815388797,
                43.7375229917451
            ]
        ],
        [
            [
                7.42792806024,
                43.73789341470609
            ],
            [
                7.4279663,
                43.7379099
            ],
            [
                7.428019971595817,
                43.73793601181055
            ]
        ],
        [
            [
                7.428019971595817,
                43.73793601181055
            ],
            [
                7.428110736186015,
                43.73798016976316
            ]
        ],
        [
            [
                7.428110736186015,
                43.73798016976316
            ],
            [
                7.428201136907358,
                43.73802415068964
            ]
        ],
        [
            [
                7.428201136907358,
                43.73802415068964
            ],
            [
                7.428291143186496,
                43.73806793971573
            ]
        ],
        [
            [
                7.428291143186496,
                43.73806793971573
            ],
            [
                7.428380744428465,
                43.73811153168687
            ]
        ],
        [
            [
                7.428380744428465,
                43.73811153168687
            ],
            [
                7.428469929389138,
                43.73815492113266
            ]
        ],
        [
            [
                7.428469929389138,
                43.73815492113266
            ],
            [
                7.4285223,
                43.7381804
            ],
            [
                7.428555815718708,
                43.73820298521547
            ]
        ],
        [
            [
                7.428555815718708,
                43.73820298521547
            ],
            [
                7.428637231373905,
                43.73825784872985
            ]
        ],
        [
            [
                7.428637231373905,
                43.73825784872985
            ],
            [
                7.428718293198327,
                43.73831247380852
            ]
        ],
        [
            [
                7.428718293198327,
                43.73831247380852
            ],
            [
                7.428798963313834,
                43.73836683492656
            ]
        ],
        [
            [
                7.424239815388797,
                43.7375229917451
            ],
            [
                7.424351171479866,
                43.73751467126952
            ]
        ],
        [
            [
                7.428798963313834,
                43.73836683492656
            ],
            [
                7.428879255243874,
                43.738420941197
            ]
        ],
        [
            [
                7.428879255243874,
                43.738420941197
            ],
            [
                7.4289203,
                43.7384486
            ],
            [
                7.428953502648698,
                43.73848157408526
            ]
        ],
        [
            [
                7.428953502648698,
                43.73848157408526
            ],
            [
                7.429021532863969,
                43.7385491359879
            ]
        ],
        [
            [
                7.429021532863969,
                43.7385491359879
            ],
            [
                7.429089304031803,
                43.73861644062635
            ]
        ],
        [
            [
                7.429089304031803,
                43.73861644062635
            ],
            [
                7.4291382,
                43.738665
            ],
            [
                7.429155019690634,
                43.73868508392616
            ]
        ],
        [
            [
                7.429155019690634,
                43.73868508392616
            ],
            [
                7.429215859376523,
                43.73875773090798
            ]
        ],
        [
            [
                7.429215859376523,
                43.73875773090798
            ],
            [
                7.429276515175023,
                43.73883015831496
            ]
        ],
        [
            [
                7.429276515175023,
                43.73883015831496
            ],
            [
                7.429336941359315,
                43.73890231154598
            ]
        ],
        [
            [
                7.429336941359315,
                43.73890231154598
            ],
            [
                7.4293845,
                43.7389591
            ],
            [
                7.42939522778591,
                43.73897554789505
            ]
        ],
        [
            [
                7.42939522778591,
                43.73897554789505
            ],
            [
                7.429446232165925,
                43.73905374806576
            ]
        ],
        [
            [
                7.424351171479866,
                43.73751467126952
            ],
            [
                7.424462343233103,
                43.73750636456757
            ]
        ],
        [
            [
                7.429446232165925,
                43.73905374806576
            ],
            [
                7.429497079840205,
                43.73913170797444
            ]
        ],
        [
            [
                7.429497079840205,
                43.73913170797444
            ],
            [
                7.429547759162156,
                43.7392094097645
            ]
        ],
        [
            [
                7.429547759162156,
                43.7392094097645
            ],
            [
                7.4295923,
                43.7392777
            ],
            [
                7.429596535017541,
                43.7392876587716
            ]
        ],
        [
            [
                7.429596535017541,
                43.7392876587716
            ],
            [
                7.429632547442486,
                43.73937234307608
            ]
        ],
        [
            [
                7.429632547442486,
                43.73937234307608
            ],
            [
                7.429668473568205,
                43.73945682444533
            ]
        ],
        [
            [
                7.429668473568205,
                43.73945682444533
            ],
            [
                7.429704338562282,
                43.73954116206168
            ]
        ],
        [
            [
                7.429704338562282,
                43.73954116206168
            ],
            [
                7.4297132,
                43.739562
            ],
            [
                7.429732409697104,
                43.73962798669538
            ]
        ],
        [
            [
                7.429732409697104,
                43.73962798669538
            ],
            [
                7.429757922878332,
                43.73971562631348
            ]
        ],
        [
            [
                7.429757922878332,
                43.73971562631348
            ],
            [
                7.429783435691189,
                43.73980326466619
            ]
        ],
        [
            [
                7.429783435691189,
                43.73980326466619
            ],
            [
                7.4298033,
                43.7398715
            ],
            [
                7.429807322457478,
                43.73989104434216
            ]
        ],
        [
            [
                7.424462343233103,
                43.73750636456757
            ],
            [
                7.424573330291323,
                43.73749807166595
            ]
        ],
        [
            [
                7.429807322457478,
                43.73989104434216
            ],
            [
                7.429825655929249,
                43.73998012313222
            ]
        ],
        [
            [
                7.429825655929249,
                43.73998012313222
            ],
            [
                7.429843993855402,
                43.74006922356526
            ]
        ],
        [
            [
                7.429843993855402,
                43.74006922356526
            ],
            [
                7.429872,
                43.7402053
            ]
        ],
        [
            [
                7.424573330291323,
                43.73749807166595
            ],
            [
                7.424684128069856,
                43.73748979290723
            ]
        ],
        [
            [
                7.424684128069856,
                43.73748979290723
            ],
            [
                7.424794733990175,
                43.73748152848405
            ]
        ]
        ];

        function GetMap() {
            //Initialize a map instance.
            map = new atlas.Map('myMap', {
                center: [7.42, 43.73],
                zoom: 14,
                view: 'Auto',

                //Add authentication details for connecting to Azure Maps.
                authOptions: {
                    authType: 'subscriptionKey',
                    subscriptionKey: 'subscriptionKey'
                }
            });

            //Wait until the map resources are ready.
            map.events.add('ready', function () {                

                //Create a data source and add it to the map.
                datasource = new atlas.source.DataSource();
                map.sources.add(datasource);
                
                sections.forEach((section) => {
                    line = new atlas.Shape(new atlas.data.LineString(section));
                    //Add the data to the data source.
                    datasource.add(line);
                });                

                //Add a layer for rendering line data. 
                map.layers.add(new atlas.layer.LineLayer(datasource, null, {
                    strokeColor: 'blue',
                    strokeWidth: 5,
                }));
            });

        }
    </script>
</head>
<body onload="GetMap()">
    <div id="myMap" style="position:relative;width:100%;min-width:290px;height:600px;"></div>
</body>
</html>