【问题标题】:Google Heat Map display only one place谷歌热图只显示一个地方
【发布时间】:2017-10-13 11:28:56
【问题描述】:

我有列表的后端,我有 2 个地方。

这是后端代码。

public JsonResult GetData()
    {

        List<Park> stations = new List<Park>();

        stations.Add(new Park()
        {
            Id = 2,
            GeoLat = 37.608644,
            GeoLong = 55.75226,
            Weight = 12
        });

        stations.Add(new Park()
        {
            Id = 3,
            GeoLat = 30.5807913,
            GeoLong = 50.493239,

            Weight = 12
        });

        return Json(stations.ToArray(), JsonRequestBehavior.AllowGet);
    }

我像这样在客户端得到它

 function getPoints() {

        $.getJSON('@Url.Action("GetData", "Home")',
            function (data) {
                var marker;
                // Проходим по всем данным и устанавливаем для них маркеры
                $.each(data,
                    function(i, item) {
                         marker = [
                            {
                                'location': new google.maps.LatLng(item.GeoLong, item.GeoLat),
                                'map': map,
                                'weight': item.Weight
                            }
                        ];
                    });



                var pointArray = new google.maps.MVCArray(marker);
                console.log(pointArray);
                heatmap = new google.maps.visualization.HeatmapLayer({
                    data: pointArray
                });
                heatmap.setMap(map);


            });
    };

并在此方法中更新地图

 function initMap() {
        map = new google.maps.Map(document.getElementById('map'),
            {
                zoom: 13,
                center: { lat: 55.752622, lng: 37.617567 },
                mapTypeId: 'satellite'
            });
        getPoints();
    }

但我只看到一个地方。我不明白为什么,因为我在这一行设置了断点

return Json(stations.ToArray(), JsonRequestBehavior.AllowGet);

我返回 2 个位置。

我的问题可能出在哪里?

谢谢你的帮助

【问题讨论】:

    标签: javascript c# asp.net asp.net-mvc google-maps-api-3


    【解决方案1】:
    var marker;
    

    应该改为:

    var marker = [];
    

    marker = [
        {
            'location': new google.maps.LatLng(item.GeoLong, item.GeoLat),
            'map': map,
            'weight': item.Weight
        }
    ];
    

    应该改为:

    marker.push({
        'location': new google.maps.LatLng(item.GeoLong, item.GeoLat),
        'map': map,
        'weight': item.Weight
    });
    

    您现有代码的问题是您分配marker 单个元素数组(因此您实际上只会显示最后一个标记)。您目前没有向该数组添加。我的更改使用push 来解决这个问题。

    【讨论】:

    • 是的。它有助于。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2018-07-22
    • 2013-04-20
    相关资源
    最近更新 更多