1

我有 asp.net 网页表单页面,它有中继器在页面上显示多个标记。我想对标记进行聚类,我按照网上找到的一些示例进行操作,但到目前为止没有成功。这就是我所拥有的:

  1. html标记:


   var markers = [
        <asp:Repeater ID="rptMarkers" runat="server">
        <ItemTemplate>
                 {
                     "title": '<%# Eval("Name") %>',
                     "lat": '<%# Eval("Latitude") %>',
                     "lng": '<%# Eval("Longitude") %>',
                     "description": '<%# Eval("Name") %>'
                 }
    </ItemTemplate>
    <SeparatorTemplate>
        ,
    </SeparatorTemplate>
    </asp:Repeater>
        ];
        var mapOptions = {
            center: new google.maps.LatLng(43.648399, -79.485703),
            zoom: 8,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map  = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
        var markerCluster = new MarkerClusterer(map, markers, mapOptions );
    </script>
    <script type="text/javascript">

        window.onload = function () {
            var mapOptions = {
                center: new google.maps.LatLng(43.648399, -79.485703),
                zoom: 8,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            var infoWindow = new google.maps.InfoWindow();
            var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
            for (i = 0; i < markers.length; i++) {
                var data = markers[i]
                var myLatlng = new google.maps.LatLng(data.lat, data.lng);
                var marker = new google.maps.Marker({
                    position: myLatlng,
                    map: map,
                    title: data.title
                });
                (function (marker, data) {
                    google.maps.event.addListener(marker, "click", function (e) {
                        infoWindow.setContent(data.description);
                        infoWindow.open(map, marker);
                    });
                })(marker, data);
            }
        }
    </script>
    <div id="dvMap" style="width: 500px; height: 277px">
    </div>


            </div>

然后,在后端文件中,我从数据库中获取所有数据并发送到正确呈现多个标记的转发器,您也可以在此屏幕截图中看到:http: //gyazo.com/049821984b889eefa551df663af0d2eb.png

这是我试图用来使集群工作的部分(在上面的代码中也可见):

  var mapOptions = {
            center: new google.maps.LatLng(43.648399, -79.485703),
            zoom: 8,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map  = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
        var markerCluster = new MarkerClusterer(map, markers, mapOptions );

当我查看页面的来源时,这是我得到的关于标记数据的内容:

<div class="divdisplay">  
          <script type="text/javascript" src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/src/markerclusterer.js"></script>
                <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
    var markers = [

             {
                 "title": 'Product Specialist',
                 "lat": '43.6877731',
                 "lng": '-79.2709108',
                 "description": 'Product Specialist'
             }

    ,

             {
                 "title": 'Finance Manager',
                 "lat": '43.7345524',
                 "lng": '-79.2561590',
                 "description": 'Finance Manager'
             }

    ,

             {
                 "title": 'New Job',
                 "lat": '43.7598255',
                 "lng": '-79.2252689',
                 "description": 'New Job'
             }

    ,

             {
                 "title": 'AD, Algorithmic Trading Development Team ',
                 "lat": '43.7240344',
                 "lng": '-79.3006776',
                 "description": 'AD, Algorithmic Trading Development Team '
             }

    ,

             {
                 "title": 'Financial Analyst - Investment Finance Systems Ana',
                 "lat": '43.7870613',
                 "lng": '-79.2761657',
                 "description": 'Financial Analyst - Investment Finance Systems Ana'
             }

    ,

             {
                 "title": 'New Business Coordinator CSR - Group Benefits',
                 "lat": '43.7891500',
                 "lng": '-79.1404511',
                 "description": 'New Business Coordinator CSR - Group Benefits'
             }

    ,

             {
                 "title": 'TD Job 1',
                 "lat": '43.7092267',
                 "lng": '-79.2962398',
                 "description": 'TD Job 1'
             }

    ,

             {
                 "title": 'Web Designer',
                 "lat": '43.7841207',
                 "lng": '-79.2917106',
                 "description": 'Web Designer'
             }

    ,

             {
                 "title": 'ceo',
                 "lat": '43.6488293',
                 "lng": '-79.3965691',
                 "description": 'ceo'
             }

    ,

             {
                 "title": 'IT Analyst',
                 "lat": '43.6546566',
                 "lng": '-79.3805941',
                 "description": 'IT Analyst'
             }

    ,

             {
                 "title": 'Part-Time MARKET RESEARCH INTERVIEWERS',
                 "lat": '43.6683729',
                 "lng": '-79.4392835',
                 "description": 'Part-Time MARKET RESEARCH INTERVIEWERS'
             }

    ];    

    var mapOptions = {
        center: new google.maps.LatLng(43.648399, -79.485703),
        zoom: 8,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map  = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
    var mcOptions = {gridSize: 50, maxZoom: 15};
    var markerCluster = new MarkerClusterer(map, markers, mcOptions);
</script>
<script type="text/javascript">

    window.onload = function () {
        var mapOptions = {
            center: new google.maps.LatLng(43.648399, -79.485703),
            zoom: 8,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var infoWindow = new google.maps.InfoWindow();
        var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
        for (i = 0; i < markers.length; i++) {
            var data = markers[i]
            var myLatlng = new google.maps.LatLng(data.lat, data.lng);
            var marker = new google.maps.Marker({
                position: myLatlng,
                map: map,
                title: data.title
            });
            (function (marker, data) {
                google.maps.event.addListener(marker, "click", function (e) {
                    infoWindow.setContent(data.description);
                    infoWindow.open(map, marker);
                });
            })(marker, data);
        }
    }
</script>
<div id="dvMap" style="width: 500px; height: 277px">
</div>


        </div>

非常欢迎每一个建议。谢谢,拉齐尔

4

1 回答 1

2

您需要将 google.maps.Marker 对象数组传递给标记聚类器 - 您正在传递初始标记数据数组。

您需要在创建所有标记之后创建一个新的 MarkerClusterer,而不是之前。因此,请从示例中的第一个脚本块中删除这些行:

var mcOptions = {gridSize: 50, maxZoom: 15};
var markerCluster = new MarkerClusterer(map, markers, mcOptions);

我们将在创建所有标记后将它们移动到。因此,将您的 window.onload 函数更改为:

<script type="text/javascript">
window.onload = function () {
    var mapOptions = {
        center: new google.maps.LatLng(43.648399, -79.485703),
        zoom: 8,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };


    // declare an array to keep all google.maps.Marker instances in:
    var googleMarkers = []; 


    var infoWindow = new google.maps.InfoWindow();
    var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
    for (i = 0; i < markers.length; i++) {
        var data = markers[i]
        var myLatlng = new google.maps.LatLng(data.lat, data.lng);
        var marker = new google.maps.Marker({
            position: myLatlng,
            map: map,
            title: data.title
        });



        // add the new marker to the googleMarkers array
        googleMarkers.push(marker);



        (function (marker, data) {
            google.maps.event.addListener(marker, "click", function (e) {
                infoWindow.setContent(data.description);
                infoWindow.open(map, marker);
            });
        })(marker, data);
    }


    // now all the markers have been created, make a new MarkerClusterer:
    var mcOptions = {gridSize: 50, maxZoom: 15};
    var markerCluster = new MarkerClusterer(map, googleMarkers, mcOptions);

}
</script>
于 2013-11-08T11:43:49.013 回答