ShaMoh
ShaMoh

Reputation: 1570

Bing Map V8 Cluster Pass real time data

I am just starting with Bing map. Gone through few examples in official documentation.

Example from 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>

In above bing map cluster example how to replace the TestDataGenerator data with realtime data JSON like below

    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}];

When I pass above object in ClusterLayer I am getting following error

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

Upvotes: 3

Views: 646

Answers (1)

rbrundritt
rbrundritt

Reputation: 18004

You have to loop through your data and turn it into pushpins. Here's a code sample:

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.

Upvotes: 1

Related Questions