Search code examples
javascriptazuregeolocationmapsazure-maps

How to tune Azure Maps to get the corract location?


I am interested in Azure Maps' animation library. After setting up an account and initialized the map. I found out that the coordinates I put as parameters aren't the ones it shows. For Example I currently live in Tunisia. So I've set up the center coordinates to (36.800697, 10.181198). But, the map gets centered at Addis Ababa for some reason. I am currently following their code example "Animate marker along path". [Github link][1] As you can see I have set up a bunch of mock coordinates. PS: In the following example, I will mask my key.

 <!DOCTYPE html>
<html lang="en">
<head>
    <title>Animate marker along path - Azure Maps Web SDK Samples</title>

    <meta charset="utf-8" />
    <link rel="shortcut icon" href="/favicon.ico"/>
    <meta http-equiv="x-ua-compatible" content="IE=Edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <meta name="description" content="This sample shows how to easily animate a HTML marker along a path on the map." />
    <meta name="keywords" content="Microsoft maps, map, gis, API, SDK, animation, animate, animations, point, symbol, pushpin, marker, pin" />
    <meta name="author" content="Microsoft Azure Maps" />

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

    <!-- Add reference to the animation module. -->
    <script src="../Common/scripts/azure-maps-animations.min.js"></script>

    <script type='text/javascript'>
        var map, marker, animation;

        //Create an array of points to define a path to animate along.
        var path = [
            [36.80067, 10.19111],
            [36.80081, 10.19077],
            [36.80148, 10.19548],
            [36.83469, 10.30360]
        ];

        function GetMap() {
            //Initialize a map instance.
            map = new atlas.Map('myMap', {
                center: [36.817421, 10.305192],
                zoom: 13,
                view: 'Auto',
                
        //Add your Azure Maps key to the map SDK. Get an Azure Maps key at https://azure.com/maps. NOTE: The primary key should be used as the key.
                authOptions: {
                    authType: 'subscriptionKey',
                    subscriptionKey: '<key>'
                }
            });

            //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);

                //Add a line for the path as a visual reference.
                datasource.add(new atlas.data.LineString(path));
           
                //Add a layer for rendering line data. 
                map.layers.add(new atlas.layer.LineLayer(datasource));

                //Create a marker and add it to the map.
                marker = new atlas.HtmlMarker({
                    position: [-122.33825, 47.53945]
                });
                map.markers.add(marker);

                map.events.add('click', function () {
                    if (animation) {
                        //Restart the animation.
                        animation.reset();
                        animation.play();
                    } else {
                        animation = atlas.animations.moveAlongPath(path, marker, { duration: 2000, captureMetadata: true, autoPlay: true });
                    }
                });
            });
        }
    </script>
</head>
<body onload="GetMap()">
    <div id="myMap" style="position:relative;width:100%;min-width:290px;height:600px;"></div>

    <div style="position:absolute;top:0px;left:calc(50% - 100px);background-color:white;padding:5px;">Click the map to animate marker.</div>

    <fieldset style="width:calc(100% - 30px);min-width:290px;margin-top:10px;">
        <legend><h1 style="font-size:16px">Animate marker along path</h1></legend>
        This sample shows how to easily animate a HTML marker along a path on the map.
        This sample uses the open source <a href="https://github.com/Azure-Samples/azure-maps-animations" target="_blank">Azure Maps Animation module</a>
    </fieldset>
</body>
</html>

Solution

  • You inverted longitude and latitude. The azure-maps library expects the longitude first :

    map = new atlas.Map('myMap', {
                    center: [10.305192, 36.817421],
                    zoom: 13,
                    view: 'Auto',
                    
            //Add your Azure Maps key to the map SDK. Get an Azure Maps key at https://azure.com/maps. NOTE: The primary key should be used as the key.
                    authOptions: {
                        authType: 'subscriptionKey',
                        subscriptionKey: '<key>'
                    }
                });