javascript - 使用 JavaScript 禁用缩放谷歌地图?

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

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

禁用了它
zoomControl: false,

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

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

我试过 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>

如有任何帮助,我们将不胜感激。

最佳答案

你可能想试试:

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>

关于javascript - 使用 JavaScript 禁用缩放谷歌地图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28138510/

相关文章:

objective-c - 将 CLLocation 保存到 plist

ios - 如何使用 ARC 在 iOS 7 中存储密码

ios - 如何检测我是否拥有 iPhone 2G,3G,3GS

javascript - 你如何使用 jQuery 使元素交替颜色?

javascript - Css3 slider 多图像

javascript - 如何使用 Cordova 访问 Android 中的通话记录?

android - SwipeRefreshLayout 应用主题

java - 使用 NFC TAG 检测我的应用程序的问题

javascript - 使用 Canvas HTML 生成 map 网格和图像的问题

javascript - 与 DIV 的水平滚动条作斗争