【问题标题】:How to centralized the heat-map area in the map after zoom-in?放大后如何集中地图中的热图区域?
【发布时间】:2016-03-15 20:04:50
【问题描述】:

我正在使用谷歌地图 API 来显示热图。缩放一级后,我希望热图区域显示在地图的中心。我还附上了图片,您可以在地图底部看到热图。

render: function() {
    var results = this.model.get('results');
    if (!results)
            return this;

    var data = results[this.model.get('index')];
    if (!data)
            return this;

    if (!data.data.length) {
        data.data.push({
            count: 0,
            lat: 0,
            lng: 0
        });
    }
    showHeatMapData(data);
    var startTime = this.model.get("starttm");
    if(startTime >= 1436985000 && startTime <= 1437244199)
    {
        $("#pichart").show();
    }
    else
    {
        $("#pichart").hide();
    }

    this.heatmap.setData(data);
    this.map.setZoom(20);
    return this;
}

【问题讨论】:

标签: javascript google-maps google-maps-api-3 heatmap


【解决方案1】:

缩放地图并使其适合所有点:

this.heatmap.setData(data);
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < data.length; i++) {
    bounds.extend(data[i]);
}
this.map.fitBounds(bounds);

【讨论】:

    【解决方案2】:

    试试这个:

    this.heatmap.setData(data);
    
    var bounds = new google.maps.LatLngBounds();
    
    for (var i = 0; i < data.length; i++) {
        bounds.extend(data[i]);
    }
    
    this.map.setCenter(bounds.getCenter());
    this.map.setZoom(20);
    

    【讨论】:

    • 我用我的热图试过了,效果也很好,你的答案的问题是缩放不一定正确设置取决于点的分散,顺便说一句,我赞成你的答案很有帮助,只是更好地使用 map.fitBounds 就像在其他问题和答案中一样
    【解决方案3】:

    我猜你有一个经度和纬度包含数组 有些东西看起来像

     var locationarray = [];
    
     locationarray[0] = new google.maps.LatLng(13.74428, 100.5404525);
     locationarray[1] = new google.maps.LatLng(13.744108, 100.543098);
    

    现在,当您加载完其他功能后,您可以简单地从您的位置数组中设置地图中心,就像这样

    map.setCenter(locationarray[0]);
    

    希望对你有帮助。

    【讨论】:

    • 他有一个坐标数组,只是将中心设置为其中的第一个,而不是热图的中心
    猜你喜欢
    • 2013-11-12
    • 1970-01-01
    • 1970-01-01
    • 2013-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-30
    相关资源
    最近更新 更多