disable pinch and zoom in google maps using JavaScript?

為{幸葍}努か 提交于 2019-12-13 21:04:57

问题


I'm using the google maps in one of my projects but I don't want to have zooming affect in the map so I disabled it via

zoomControl: false,

however, this only works for desktops and laptops as they dont support multi touch.

when i view my map in a device that supports multi touch like tablets or smart phones, I can zoom in using pinch and zoom on the device.

I did try map2.getUiSettings().setZoomControlsEnabled(false); which has no affect on it and I still can pinch and zoom!

I also tried map.getUiSettings().setZoomControlsEnabled(false); to see if that makes any different but it doesn't!

could someone please advise on this issue?

This is my full code:

                <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>

any help would be appreciated.


回答1:


You might want to try:

var tblock = function (e) {
    if (e.touches.length > 1) {
        e.preventDefault()
    }

    return false;
}

document.body.addEventListener("touchmove", tblock, true);

Related:

Disabling pinch-zoom on google maps [Desktop]

Google Maps API v3 Disable Pinch to Zoom on iPad Safari

Edit (working example on 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>



回答2:


Add this code to your mapOptions:

minZoom: 10,
maxZoom: 10

Basically, set minZoom and maxZoom to the same value to disable zoom on your map.



来源:https://stackoverflow.com/questions/28138510/disable-pinch-and-zoom-in-google-maps-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!