StephanGeorg / staticmaps

A Node.js library for creating map images with markers, polylines, polygons and text.
MIT License
170 stars 49 forks source link

Support quadkey urls (Bing Maps) #65

Closed BKSnake closed 2 years ago

BKSnake commented 2 years ago

Hello

Could you help me to understand? Ho can I use this lib with Bing Maps. I need generate some image and i have Key to Bing Maps

Thanks

StephanGeorg commented 2 years ago

Bing Maps Tiles Server uses "quadkeys" to address a specific tile. Currently staticmaps supports the xyz schema only thus does not support Bing Maps, sorry.

Feel free to do a PR with quadKeys implementation yourself. I'm happy to help you with it. Implementation should be relatively easy.

C# implementation source

        public static string TileXYToQuadKey(int tileX, int tileY, int levelOfDetail)  
        {  
            StringBuilder quadKey = new StringBuilder();  
            for (int i = levelOfDetail; i > 0; i--)  
            {  
                char digit = '0';  
                int mask = 1 << (i - 1);  
                if ((tileX & mask) != 0)  
                {  
                    digit++;  
                }  
                if ((tileY & mask) != 0)  
                {  
                    digit++;  
                    digit++;  
                }  
                quadKey.Append(digit);  
            }  
            return quadKey.ToString();  
        } 

JavaScript implementation source

const tileXYToQuadKey = (x, y, z) => {
    var quadKey = [];
    for (var i = z; i > 0; i--) {
        var digit = '0';
        var mask = 1 << (i - 1);
        if ((x & mask) != 0) {
            digit++;
        }
        if ((y & mask) != 0) {
            digit++;
            digit++;
        }
        quadKey.push(digit);
    }
    return quadKey.join('');
}
StephanGeorg commented 2 years ago

staticmaps@1.9.1 now supports {quadkeys} and Bing Maps.

https://github.com/StephanGeorg/staticmaps/blob/06a73a6401efb079ff2d2ca4c8e0225b48cf7a4c/test/staticmaps.js#L38-L46