Bing Map V8 Cluster传递实时数据

时间:2016-12-01 11:14:29

标签: maps bing-maps

我刚开始使用Bing地图。在官方文档中通过几个例子。

来自Bing map V8 official documentation

的示例
<!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">
    var map, clusterLayer;

    function GetMap() {
        map = new Microsoft.Maps.Map('#myMap',{
            credentials: 'Your Bing Maps Key',
            zoom: 3
        });

        Microsoft.Maps.loadModule("Microsoft.Maps.Clustering", function () {
            //Generate 3000 random pushpins in the map view.
            var pins = Microsoft.Maps.TestDataGenerator.getPushpins(3000, map.getBounds());

            //Create a ClusterLayer with options and add it to the map.
            clusterLayer = new Microsoft.Maps.ClusterLayer(pins, {
                clusteredPinCallback: customizeClusteredPin
            });
            map.layers.insert(clusterLayer);
        });
    }

    function customizeClusteredPin(cluster) {
           //Add click event to clustered pushpin
        Microsoft.Maps.Events.addHandler(cluster, 'click', clusterClicked);
    }

    function clusterClicked(e) {
        if (e.target.containedPushpins) {
            var locs = [];
            for (var i = 0, len = e.target.containedPushpins.length; i < len; i++) {
                //Get the location of each pushpin.
                locs.push(e.target.containedPushpins[i].getLocation());
            }

            //Create a bounding box for the pushpins.
            var bounds = Microsoft.Maps.LocationRect.fromLocations(locs);

            //Zoom into the bounding box of the cluster. 
            //Add a padding to compensate for the pixel area of the pushpins.
            map.setView({ bounds: bounds, padding: 100 });
        }
    }
    </script>
</head>
<body>
    <div id="myMap" style="position:relative;width:600px;height:400px;"></div>
</body>
</html>

在上面的bing map集群示例中,如何使用下面的实时数据JSON替换TestDataGenerator数据

    mapData = [{"Name":"Point: 0","Latitude":22.0827,"Longitude":80.2707},
               {"Name":"Point: 1","Latitude":24.0827,"Longitude":80.2707},
               {"Name":"Point: 2","Latitude":26.0827,"Longitude":80.2707},
               {"Name":"Point: 3","Latitude":28.0827,"Longitude":80.2707},
               {"Name":"Point: 4","Latitude":20.0827,"Longitude":80.2707},
               {"Name":"Point: 5","Latitude":22.0827,"Longitude":82.2707},
               {"Name":"Point: 6","Latitude":30.0827,"Longitude":80.2707},
               {"Name":"Point: 7","Latitude":22.0827,"Longitude":84.2707},
               {"Name":"Point: 8","Latitude":32.0827,"Longitude":84.2707},
               {"Name":"Point: 9","Latitude":18.0827,"Longitude":80.2707}];

当我在ClusterLayer中传递上述对象时,我收到以下错误

Uncaught TypeError: i[t].getLocation is not a function(…)

1 个答案:

答案 0 :(得分:1)

您必须循环浏览数据并将其转换为图钉。这是一个代码示例:

var pins = [];

for(var i = 0;i < mapData.length;i++){
    var pin = new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location(mapData[i].Latitude, mapData[i].Longitude));

    //Store the original data object in the pushpins metadata so that you can access other properties like Name.
    pin.metedata = mapData[i];

    pins.push(pin);
}

//Now "pins" is an array of pushpins. Add them to the map or to the clustering layer.