【问题标题】:How to center google map v3 using jquery.gmap.js如何使用 jquery.gmap.js 将谷歌地图 v3 居中
【发布时间】:2013-09-24 09:01:18
【问题描述】:

如何使用 jquery.gmap.js (http://labs.mario.ec/jquery-gmap/jquery.gmap.js) 将我的谷歌地图 v3 居中(视点)。 我在同一个城市有两个或多个标记,但我想在地图上的特定点居中地图(或在第一个标记上居中坐标)。 我的 jquery 是:

$(document).ready(function()
{  
 $('#map_canvas').gMap({            
    zoom: 16,
    markers:[
        {
            latitude: 51.51793,
            longitude: -0.140419,
            html: 'some_text_1',
            popup: true,                    
        },
        {
            latitude: 51.52028,
            longitude: -0.122766,
            html: 'some_text_2',
            popup: true,    
        }
    ]
});

});

【问题讨论】:

    标签: jquery google-maps google-maps-api-3 center


    【解决方案1】:

    您需要将 'popup' 的值设置为 false,否则 gmap 会将第二个标记居中。以下是更新后的js代码。

    $(document).ready(function () {
        $('#map_canvas').gMap({
            zoom: 16,
            markers: [{
                latitude: 51.51793,
                longitude: -0.140419,
                html: 'some_text_1',
                popup: false,
            }, {
                latitude: 51.52028,
                longitude: -0.122766,
                html: 'some_text_2',
                popup: true,
            }]
        });
    
        $('#map_canvas').gMap('centerAt', {
                 latitude: 51.51793,
                longitude: -0.140419,
                zoom: 16
            });
    });
    

    这是小提琴...gmap-fiddle

    【讨论】:

    • 谢谢,如果我还有一两个标记并且我需要“popup: true”,它会居中最后一个标记,对吗?
    【解决方案2】:

    您可以通过在 .gMap({ ... }) 之后调用以下行来完成此操作

    $('#map_canvas').trigger('gMap.centerAt', [lat, lng, zoom])
    

    【讨论】:

    • 原来是这样的,对吗? "$(document).ready(function() { $('#map_canvas').gMap({ ... } ); $('#map_canvas').trigger('gMap.centerAt', [51.51793, -0.140419 ]); });"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多