【问题标题】:disable pinch and zoom in google maps using JavaScript?使用JavaScript禁用捏和放大谷歌地图?
【发布时间】:2015-01-25 16:17:21
【问题描述】:

我在我的一个项目中使用谷歌地图,但我不想在地图中产生缩放效果,所以我通过

禁用了它
zoomControl: false,

但是,这仅适用于台式机和笔记本电脑,因为它们不支持多点触控。

当我在支持多点触控的设备(如平板电脑或智能手机)中查看我的地图时,我可以在设备上使用捏合和缩放进行放大。

我确实尝试了map2.getUiSettings().setZoomControlsEnabled(false);,这对它没有任何影响,我仍然可以捏和缩放!

我也试过map.getUiSettings().setZoomControlsEnabled(false); 看看是否有什么不同,但没有!

有人可以就这个问题提出建议吗?

这是我的完整代码:

                <script>


            function showCurrentLocation(position) {
                var latitude = <?php echo $curLat; ?>;
                var longitude = <?php echo $curLon; ?>;
    var coords2 = new google.maps.LatLng(latitude, longitude);



    var mapOptions2 = {
        zoom: 10,
        center: coords2,
        mapTypeControl: false,
        streetViewControl: false,
        panControl: false,
        zoomControl: false,
        scrollwheel: false,
        navigationControl: false,
        mapTypeControl: false,
        scaleControl: false,
        draggable: true,
        mapTypeId: google.maps.MapTypeId.ROADMAP

    };


    //create the map, and place it in the HTML map div
    map2 = new google.maps.Map(
    document.getElementById("mapPlaceholder"), mapOptions2);

    //place the initial marker
    var marker2 = new google.maps.Marker({
        position: coords2,
        map: map2,
        title2: "Current location!"
    });
}
google.maps.event.addDomListener(window,'load',showCurrentLocation);

map2.getUiSettings().setZoomControlsEnabled(false);
        </script>

任何帮助将不胜感激。

【问题讨论】:

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


    【解决方案1】:

    你可能想试试:

    var tblock = function (e) {
        if (e.touches.length > 1) {
            e.preventDefault()
        }
    
        return false;
    }
    
    document.body.addEventListener("touchmove", tblock, true);
    

    相关:

    Disabling pinch-zoom on google maps [Desktop]

    Google Maps API v3 Disable Pinch to Zoom on iPad Safari

    编辑(Android 4.4.2 上的工作示例):

    <!DOCTYPE html>
    <html>
    <head>
    <style type="text/css">
    html, body, #mapPlaceholder {
        height: 100%;
        margin: 0;
        padding: 0;
    }
    </style>
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
    <script type="text/javascript">
          var map2;
    
          function initialize() {
    
            var coords2 = new google.maps.LatLng(38.8, -77);
    
                var mapOptions2 = {
                    zoom: 10,
                    center: coords2,
                    mapTypeControl: false,
                    streetViewControl: false,
                    panControl: false,
                    zoomControl: false,
                    scrollwheel: false,
                    navigationControl: false,
                    mapTypeControl: false,
                    scaleControl: false,
                    draggable: true,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };
    
    
                //create the map, and place it in the HTML map div
                map2 = new google.maps.Map(document.getElementById("mapPlaceholder"), mapOptions2);
    
                //place the initial marker
                var marker2 = new google.maps.Marker({
                    position: coords2,
                    map: map2,
                    title: "Current location!"
                });
                 var tblock = function (e) {
                if (e.touches.length > 1) {
                    e.preventDefault()
                }
    
                return false;
            }
    
            document.body.addEventListener("touchmove", tblock, true);
          }
          google.maps.event.addDomListener(window, 'load', initialize);
    
    </script>
    </head>
    <body>
        <div id="mapPlaceholder"></div>
    </body>
    </html>
    

    【讨论】:

    • 我已经在许多其他网站以及 STO 上发现了这一点!在我的情况下它不起作用。
    • 根据一些调查:getUiSettings() 用于 Android Google Maps API,因此不能与 JavaScript API 一起使用。我测试了上面的代码并将其放在showCurrentLocation 方法中,它似乎可以工作。请参阅我编辑的答案。
    【解决方案2】:

    将此代码添加到您的地图选项中:

    minZoom: 10,
    maxZoom: 10
    

    基本上,将 minZoom 和 maxZoom 设置为相同的值以禁用地图缩放。

    【讨论】:

      猜你喜欢
      • 2013-02-10
      • 2018-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多