How can JavaScript be used to prevent pinch and zoom functionality on Google Maps?

In my project, I am utilizing Google Maps but I want to disable the zooming effect on the map. To achieve this, I have disabled the zoom control using zoomControl: false,. However, this modification only applies to desktops and laptops as they do not support multi-touch functionality.

When accessing the map on devices such as tablets or smartphones that support multi-touch, I am still able to pinch and zoom despite the previous settings.

I attempted using

map2.getUiSettings().setZoomControlsEnabled(false);
with no success in preventing pinch and zoom gestures.

Furthermore, I tested

map.getUiSettings().setZoomControlsEnabled(false);
to check for any differences, but the issue persisted.

If anyone could provide some guidance on this matter, it would be greatly appreciated.

Below is my complete 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 display 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 assistance on resolving this issue would be highly valued.

Answer №1

If you're looking to disable pinch-zoom on Google Maps, you can try the following JavaScript code snippet:

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

    return false;
}

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

For more information and related discussions, check out these links:

Disabling pinch-zoom on google maps [Desktop]

Google Maps API v3 Disable Pinch to Zoom on iPad Safari

Here is a working example that disables pinch-zoom on Android 4.4.2:

<!DOCTYPE html>
<html>
... // additional HTML and JavaScript code for the map

Answer №2

To implement this feature, include the following code in your mapOptions section:

minZoom: 10,
maxZoom: 10

This code snippet effectively disables zoom functionality on your map by setting both minZoom and maxZoom to the same value.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for avoiding a 500 GET error due to an empty request during page loading

I recently encountered an issue with a form containing a dependent drop-down menu that reloads after form submission to preselect the main choice that was made before submission. The problem arises when the page initially loads without any parameters being ...

Converting values into keys and vice versa in JavaScript: A comprehensive guide

I have an array of Objects where I want to convert the first value into a key and the second value into a value. Please review the question below along with my desired output: [ { "name": "Unknown", "value": "R ...

Is it possible to dynamically append and delete rows of interconnected dropdown menus?

I'm currently working on a form that allows users to add and remove rows with cascading dropdowns for class selection. Everything is functional except for the feature to remove selected classes. I've experimented with different methods like the ...

How can Bitcode be implemented in App Thinning?

Apple's iOS 9 update brought in three new ways of App Thinning: App Slicing Resource On Demand Bit Code: acts as a plug-in for iOS Apps I have come across tutorials and example codes for 1. and 2., but information on Bitcode seems to be scarc ...

Using makeStyles() in MUI for conditional rendering

Trying to dynamically change the value of backgroundColor under the property "& + $track" using props.disabled but it seems that MUI does not recognize it. Why could that be? const useStyles = makeStyles((theme) => ({ root: { overflow: "vis ...

The Highchart column chart is triggering the drilldown event multiple times

I have created a column chart using Highchart and I am facing an issue with the drilldown functionality. Whenever I click on a bar multiple times, the drilldown event triggers multiple times as well. This results in the drilldown event being triggered repe ...

Why is it that TypeScript does not issue any complaints concerning specific variables which are undefined?

I have the following example: class Relative { constructor(public fullName : string) { } greet() { return "Hello, my name is " + fullName; } } let relative : Relative = new Relative("John"); console.log(relative.greet()); Under certain circum ...

tool for showcasing data on a webpage with a specific layout

Which chart library is best suited for presenting data in the formats shown in the images below? Can HighCharts handle these formats? Does Google Charts allow for a combination of bubbles and lines? ...

Timeout for AngularJS Bootstrap UI Modal

I am having some trouble with opening a bootstrap modal using the $timeout function. The issue I am facing is that the modal keeps opening multiple times as the timeout fires repeatedly. Any assistance or suggestions on how to resolve this problem would be ...

Switching sub components based on routing through navigation links after logging in

I'm having an issue with my routing setup while transitioning from the login page to the main page. Here's how my Routes are structured: App.jsx <BrowserRouter> <Routes> <Route path="/main/" element={<Main ...

Basic Inquiry: Unhandled TypeError - Unable to access the property 'length' of an object that is not defined

Currently, I am utilizing a straightforward JSON Ajax request to fetch some JSON data. However, every time I attempt to use the JSON object, I encounter the following error: Uncaught TypeError: Cannot read property 'length' of undefined $(do ...

What could be the reason for the bottom edge of my central diagonal image having a darker border?

I can't figure out why the border on the bottom edge of my image is darker. You can check out the demo here. To get a closer look at the issue, you can open a software like GIMP and zoom in on the following image to see the difference in values: http ...

Utilizing Angular for enhanced search functionality by sending multiple query parameters

I'm currently facing an issue with setting up a search functionality for the data obtained from an API. The data is being displayed in an Angular Material table, and I have 8 different inputs that serve as filters. Is there a way to add one or more s ...

Node.js API requests often result in undefined responses

As a newcomer to Node.JS, I am currently experimenting with calling a REST API using the GET method. I have utilized the 'request' package available at this link. While the call functions correctly, I encounter an issue when attempting to return ...

What is the best way to incorporate an AppBar featuring a Back to Top button from Material UI for React into my application?

While exploring the Material UI documentation, I came across this interesting code snippet: import React from 'react'; import PropTypes from 'prop-types'; import AppBar from '@material-ui/core/AppBar'; import Toolbar from &ap ...

`How can I activate caching for getServerSideProps in Next.js?`

We have implemented server-side rendering for a few pages and components. In an attempt to optimize performance, we have been experimenting with caching API responses. export async function getServerSideProps(context) { const res = await getRequest(API ...

Creating a route provider tailored to specific user roles

I have a rather straightforward requirement. There are 3 different User Roles: CATUSER LICUSER ALLUSER The User Role value is stored in the $rootScope.userRole variable. The User Role is predefined before the AngularJS application starts as the Angula ...

Creating a button that integrates an image within a single div element

Currently, I understand the code: $('#leftDev').css("background-image", "url(img/1.png) "); will set the entire background of my leftDiv to display "1.png". However, I would like to position this image as a regular picture within the div and no ...

How can you personalize the background color of a Material-UI tooltip?

Is there a way to customize the hover tooltip with a "? icon" for users providing input guidance in a text field? I prefer the design to have white background with grey text and larger font size, as opposed to MUI's default style which is grey with wh ...

What is the proper method for effectively employing destructuring?

I'm trying to figure out how to properly return state with a fetched array using the spread operator. Here is my reducer code snippet: function themes(state = [], actions){ switch(actions.type){ case FETCH_THEMES_SUCCESSFULLY: const { th ...