Touch Events not working with Google Maps in Angular

我只是一个虾纸丫 提交于 2019-11-28 01:34:55

This is in fact a Google Map bug #6425

The fun fact is that Google Map API is not serving the same content based on the User Agent. So, for some use case, the workaround is to spoof the User Agent, ether by :

  • Chrome Dev tool > Emulation > Model
  • a user agent spoofing extension
  • --user-agent chrome flag

Any android or iOS device will work, changing the default desktop google map app to a mobile like app. Drag and pinch will work again on this mobile like app.

After some testing, the minimal UA that switches to the mobile like app is "Chrome" or "AppleWebKit/537", but it sounds hazardous to use it, as it may very well change in the future.

For my purpose, chrome based kioks, this will do the trick until the bug is fixed.

JJ Stiff

Putting the following code before the google maps api script tag helps me. But, unfortunately, it continues to disable mouse events. Please, can we a find a solution for Google Maps API with both touch and mouse support?

This solution in place of setting the application boot flags of '--user-agent chrome' or '--user-agent safari'

<script>
navigator = navigator || {};
navigator.msMaxTouchPoints = navigator.msMaxTouchPoints || 2;

//navigator.maxTouchPoints = navigator.maxTouchPoints || 2;
//window.ontouchstart = window.ontouchstart || function() {console.log('this is touchstart!');};
</script>

Note that only the first two lines are used, the other two are commented out and were not required.

My inspiration for this solution is from: https://hacks.mozilla.org/2013/04/detecting-touch-its-the-why-not-the-how/ so thank you to Patrick and Robert.

I used the above solution and modified for it to work with a mouse as well

<script>

function MapTouch() {
        return true == ("ontouchstart" in window || window.DocumentTouch && document instanceof DocumentTouch);
    }
    if (MapTouch() === true) {
        navigator = navigator || {};
        navigator.msMaxTouchPoints = navigator.msMaxTouchPoints || 2;
   }
</script>

I had the same problem.

JJ Stiff's solution enabled the touch events.

Apparently if you also want the mouse to work, you have to add the following line:

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