Point Compression Tools

 

Namespace: Microsoft.Maps.PointCompression

Microsoft.Maps.PointCompression is a static class that provides a compression algorithm to encodes/decodes a collection of Location objects into a string and back. This algorithm is used for generating a compressed collection of locations for use with the Bing Maps REST Elevation Service. This algorithm is also used for decoding the compressed coordinates returned by the GeoData API.

Note that the GeoDataAPIManager automatically converts the compressed polygon rings in the Primitives property of the response into a Polygon object for you. In addition to the REST Elevation service and the GeoData API this class can be also be used to encode/decode your own data. The encoded data is much smaller than most other formats such as GeoJSON or Well Known Text which is useful if passing data between an application and a database. The algorithm for decoding the string is documented here, and encoding is documented here.

NameDefinitionReturn ValueDescription
decodedecode(value:string)Location[]Decodes a collection of coordinates from a compressed string.
encodeencode(points:Location[])stringEncodes a collection of coordinates into a compressed string.

In this example the encoded string “x90uhio4sQmhuGwxrGz8sGp-zP5ooKpx9Eiz7Ip2vFko8E56xEl-zEyhkG” is decoded into an array of Locations. The array of locations is then displayed on the map as a Polyline and map view set accordingly so that we can see what it looks like. To demonstrate and verify the encoding/decoding logic this example then encodes the Locations and compares the two strings to ensure they are the same.

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <script type='text/javascript' 
            src='http://www.bing.com/api/maps/mapcontrol?callback=GetMap' 
            async defer></script>
    <script type='text/javascript'>
    function GetMap() {
        var map = new Microsoft.Maps.Map('#myMap', {
            credentials: ‘Your Bing Maps Key’
        });

        var encodedString = 'x90uhio4sQmhuGwxrGz8sGp-zP5ooKpx9Eiz7Ip2vFko8E56xEl-zEyhkG';

        //Decode the encoded string.
        var locs = Microsoft.Maps.PointCompression.decode(encodedString);

        //Display the locations as a polyline on the map so we can see what it looks like.
        var p = new Microsoft.Maps.Polyline(locs, {
            strokeColor: 'red',
            strokeThickness: 5
        });
        map.entities.push(p);

        //Set the view of the map over the polyline.
        map.setView({ bounds: Microsoft.Maps.LocationRect.fromLocations(locs) });

        //Encode the locations.
        var newEncodedString = Microsoft.Maps.PointCompression.encode(locs);

        //Verify that the encoded string and new encoded string match.
        if (encodedString === newEncodedString) {
            alert('Correctly encoded locations.');
        } else {
            alert('Incorrectly encoded locations.');
        }
    }
    </script>
</head>
<body>
    <div id="myMap" style="position:relative;width:600px;height:400px;"></div>
</body>
</html>

Here is what this encoded string looks like when rendered as a Polyline on top of the map. In this case it represents a section of the 520 highway in Bellevue, WA.

Line on a Map

Show: