Google 地图 v3 默认控制位置偏移

发布于 2025-01-08 01:38:14 字数 135 浏览 1 评论 0原文

我希望 Google 地图 v3 默认控件位置为“顶部,左侧”,但我希望它保留 300 像素。我还没有找到如何使用 API 来做到这一点,是吗?我想我可能只需要抓住 div 并用原始 javascript/jQuery 制作它。

提前致谢!

I want the Google Maps v3 default controls position to be 'top,left' but i want it to be left 300px. I haven't found how to do this with the API, is there? I think I may just need to grab the div and make it with raw javascript/jQuery.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

阳光①夏 2025-01-15 01:38:14

有一个更简单的解决方案。我从以下功能请求的描述中得到了这个想法 允许使用像素精确控制位置(您可以投票)。这是实现:

JavaScript:

var emptyDiv = document.createElement('div');
emptyDiv.className = 'empty';
emptyDiv.index = 0;
map.controls[google.maps.ControlPosition.TOP_LEFT].push(emptyDiv);

CSS:

.empty {
    width: 300px;
}

这是它的 JSFiddle

There's a much simpler solution. I got the idea from the description of the following feature request Allow exact control position using pixels (for which you can vote for). Here's the implementation:

JavaScript:

var emptyDiv = document.createElement('div');
emptyDiv.className = 'empty';
emptyDiv.index = 0;
map.controls[google.maps.ControlPosition.TOP_LEFT].push(emptyDiv);

CSS:

.empty {
    width: 300px;
}

And here's a JSFiddle of it.

狂之美人 2025-01-15 01:38:14

这可能并不理想,但我解决这个问题的方法是使用 jQuery。

在这里,我通过抓取 DOM 中的唯一元素并冒泡回主控制器 div 来获取没有任何独特之处的控件 div 包装器。然后我用一个自定义 div 包装它,保存该 div 的引用以供以后使用。最初我只是移动了控件 div 包装器,但它在悬停时移回到 left: 0。使用自定义 div 和偏移来包装它效果很好,并且可以稍后使用新的 div 包装。

var $controls = {}, _loaded = false;
// define the control wrapper for use later, map hasn't loaded yet

google.maps.event.addListener(map, 'idle', function(event) {
// map is idle

    if(!_loaded){ _loaded = true;
    // only runs on first idle

        var control_move = setInterval(function(){
        // look for loaded controller

            var img = 'img[src="http://maps.gstatic.com/mapfiles/mapcontrols3d7.png"]';

            if($(img).size() == 5){
            // loaded controller found

                clearInterval(control_move);
                // stop looking for loaded controller

                $(img+':first').parent().parent().parent().parent()
                    .wrap('<div id="control-offset" style="position: absolute; left: 300px; top: 0; width: 78px; height: 340px; overflow: hidden; z-index: 1000;"/>');
                $controls = $('#control-offset');
                // I wrap the controller wrap and offset that so it doesn't jump around

            }

        }, 100);
        // looping every tenth of a second

    // more initial map load code

    }

    // more idle map code

});

繁荣。告诉我这是否有意义。

It may not be ideal but the way I fixed this was with jQuery.

Here I am grabbing the control div wrapper which has nothing unique about it by grabbing a unique element in the DOM and bubbling back to the main controller div. I then wrap it with a custom div whose reference is saved for use later. Initially I just moved the control div wrapper but it moved back to left: 0 on hover. Wrapping it with a custom div and offsetting that works great and the new div wrap can be used later.

var $controls = {}, _loaded = false;
// define the control wrapper for use later, map hasn't loaded yet

google.maps.event.addListener(map, 'idle', function(event) {
// map is idle

    if(!_loaded){ _loaded = true;
    // only runs on first idle

        var control_move = setInterval(function(){
        // look for loaded controller

            var img = 'img[src="http://maps.gstatic.com/mapfiles/mapcontrols3d7.png"]';

            if($(img).size() == 5){
            // loaded controller found

                clearInterval(control_move);
                // stop looking for loaded controller

                $(img+':first').parent().parent().parent().parent()
                    .wrap('<div id="control-offset" style="position: absolute; left: 300px; top: 0; width: 78px; height: 340px; overflow: hidden; z-index: 1000;"/>');
                $controls = $('#control-offset');
                // I wrap the controller wrap and offset that so it doesn't jump around

            }

        }, 100);
        // looping every tenth of a second

    // more initial map load code

    }

    // more idle map code

});

Boom. Tell me if this makes sense.

赴月观长安 2025-01-15 01:38:14

如果您想设置垂直上边距,使用 psxls 示例,只需使用它即可:

function initialize() {
    var options = { 
        zoom: 8, 
        center: new google.maps.LatLng(-34, 150),
        zoomControl: true,
        panControl: true,
        panControlOptions: {
            position: google.maps.ControlPosition.LEFT_TOP
        },
        zoomControl: true,
        zoomControlOptions: {
             style: google.maps.ZoomControlStyle.LARGE,
             position: google.maps.ControlPosition.LEFT_TOP
        }
    };
    map = new google.maps.Map(document.getElementById('map'), options);

    var emptyDiv = document.createElement('div');
    emptyDiv.className = 'map_controls';
    emptyDiv.index = 0;
    map.controls[google.maps.ControlPosition.TOP_LEFT].push(emptyDiv);
}

google.maps.event.addDomListener(window, 'load', initialize);

http://jsfiddle .net/54LLm/19/

If you want to set the vertical top margin, using psxls example, just need to use it like:

function initialize() {
    var options = { 
        zoom: 8, 
        center: new google.maps.LatLng(-34, 150),
        zoomControl: true,
        panControl: true,
        panControlOptions: {
            position: google.maps.ControlPosition.LEFT_TOP
        },
        zoomControl: true,
        zoomControlOptions: {
             style: google.maps.ZoomControlStyle.LARGE,
             position: google.maps.ControlPosition.LEFT_TOP
        }
    };
    map = new google.maps.Map(document.getElementById('map'), options);

    var emptyDiv = document.createElement('div');
    emptyDiv.className = 'map_controls';
    emptyDiv.index = 0;
    map.controls[google.maps.ControlPosition.TOP_LEFT].push(emptyDiv);
}

google.maps.event.addDomListener(window, 'load', initialize);

http://jsfiddle.net/54LLm/19/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文