Bing Map V8 Cluster Pass实时数据 [英] Bing Map V8 Cluster Pass real time data

查看:98
本文介绍了Bing Map V8 Cluster Pass实时数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是从Bing地图开始.删除了官方文档中的几个示例.

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

来自必应地图V8官方文档

<!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地图集群示例中,如何将TestDataGenerator数据替换为如下所示的实时数据JSON

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

当我在ClusterLayer中传递对象上方时,出现以下错误

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

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

推荐答案

您必须遍历数据并将其转换为图钉.这是一个代码示例:

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.

这篇关于Bing Map V8 Cluster Pass实时数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆