The click event doesn't fire on iOS with the following JavaScript. It works fine on Windows. Why is this happening? Is there a workaround?
const pin = new google.maps.marker.PinElement({
scale: 1.0,
glyphColor: "green"
});
var testMarker = new google.maps.marker.AdvancedMarkerElement({
content: pin.element,
gmpClickable: true,
gmpDraggable: true
});
testMarker.position = new google.maps.LatLng(
34.718420, 135.556109
);
testMarker.map = mapObj;
testMarker.addListener(
'dragend',
function (event) {
alert('dragged');
}
);
testMarker.addListener(
'click',
function (event) {
alert('clicked');
}
);
On Windows + Chrome, the "clicked" alert is displayed correctly, but on iOS + Chrome (or Safari), nothing happens. Regarding "dragend", it works as expected in both cases. Using "gmp-click" instead of the "click" event produces the same results. Also, if you don't add the "dragend" event, the click event works correctly.
I found one solution: assign a pointer event to testMarker.content to control its behavior.
The previous answer did not work properly on Android + Chrome. So I improved it by storing the click location (x, y) in the 'pointermove' event and measuring the distance the pointer moved, and now it works correctly.
const pin = new google.maps.marker.PinElement({
scale: 1.0,
glyphColor: "green"
});
var testMarker = new google.maps.marker.AdvancedMarkerElement({
content: pin.element,
gmpDraggable: true,
});
testMarker.content.style.cursor = "move";
testMarker.position = new google.maps.LatLng(
34.718420, 135.556109
);
testMarker.map = mapObj;
// instead of click or gmp-click
var x, y, isDrag;
testMarker.content.addEventListener('pointerdown', (event) => {
x = event.clientX;
y = event.clientY;
isDrag = false;
});
testMarker.content.addEventListener('pointermove', (event) => {
if (event.buttons <= 0) return;
var dx = Math.abs(event.clientX - x);
var dy = Math.abs(event.clientY - y);
x = event.clientX;
y = event.clientY;
if (dx > 0 || dy > 0) {
isDrag = true;
};
});
testMarker.content.addEventListener('pointerup', (event) => {
if (! isDrag) {
testMarker.blur();
// test print
var info = document.getElementById("map_info");
info.innerHTML = "clicked";
setTimeout(()=>{
info.innerHTML = "";
}, 1000);
};
});
google.maps.event.addListener(
testMarker,
'dragend',
function (event) {
if (isDrag) {
isDrag = false;
testMarker.blur();
// test print
var info = document.getElementById("map_info");
info.innerHTML = "dragged";
setTimeout(()=>{
info.innerHTML = "";
}, 1000);
};
}
);
Thank you.