【问题标题】:Why has "reset to initial map state" button been dropped in Google Maps API v3?为什么在 Google Maps API v3 中删除了“重置为初始地图状态”按钮?
【发布时间】:2026-01-11 18:05:03
【问题描述】:

我想知道我们是否知道为什么“重置为初始地图状态”按钮/功能似乎已在 Google Maps API v3 中被删除?

在 API v2 中,箭头按钮中间的手形按钮是一种“主页​​”或“重置”按钮,可将地图重新​​调整到其初始位置和缩放级别。

当然不是世界末日,只是好奇……

【问题讨论】:

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


    【解决方案1】:

    我认为,因为开发人员自己做起来相当容易,而且它不是一个非常广泛使用的功能,所以他们可能决定不使用只有少数人使用的代码来减轻每个人的负担。

    您可以做的是在页面上添加一个自定义控件,当用户单击它时,然后将地图移回您想要的缩放和中心。收集它的一种方法可能是收听地图“空闲”事件,然后设置超时以仅在地图位置未触及 X 秒后才存储它。当然这看起来不像 v2 版本:)

    【讨论】:

    • 似乎有道理的好答案 - API v2 中“开箱即用”的其他一些小行为似乎已在 v3 中被删除。但我不能抱怨 - v3 似乎更快更轻!
    【解决方案2】:

    这是使重置按钮在 v3 上工作的小技巧。我在这里使用 jQuery。

    var attachEventToResetButton;
    
    // Attach event to the reset button
    var attachResetEvent = function(){
        var $resetImg = $('img[src*=mapcontrols3d6.png]');
    
        // We have to check if the image is available yet.
        // The reason is although the map has been loaded, the navigation might
        // take some time to load and we don't know when it will be fully loaded.
        // There doesn't seem to have an event for "Navigation loaded" in the API
        // So here is a way to work around
        if ($resetImg.length > 0)
        {
            $resetImg.css('cursor', 'pointer').attr('title', 'Return to center').click(function(){
                alert('Clicked on reset button');
    
                // Put your code to reset the map here. For example:
                //map.setMapTypeId(MAP_TYPE_ID);
                //map.setCenter(new google.maps.LatLng(LAT, LNG));
                //map.setZoom(ZOOM_LEVEL);
            });
    
            window.clearInterval(attachEventToResetButton);
        }
    }
    
    // Periodically checking to attach event to the reset button
    attachEventToResetButton = window.setInterval(attachResetEvent, 500);
    

    我所做的是,我注意到重置图像文件名是“mapcontrols3d6.png”。所以我设置了一个时间间隔来检查该图像是否已加载(即可用)。如果是,我在其中附加一个函数。

    由于这是一个 hack,它有一些问题。主要的是我们必须依靠重置图像文件名。因此,请确保 Google 不会更新此内容。

    谁有更好的办法?

    【讨论】:

    • 漂亮!我会试试。谢谢。