Search code examples
eventsopenlayers-3replicate

Replicate event pinch from a div to openlayer map


Good afternoon,

I have a div over the ol3 map with one list of marker. When the user clic on a marker, the center's map is updated with the coordinate of this marker. I tried that the user have the possibility to zoom on the map when he "pinch" on the layer on top.

I successed to intercept the event on the "layerOnTop" and replicate it to the map layer. With console.log, I saw that the job is correctly done but there is any reaction on the map.

You can see the code : http://jsfiddle.net/2ek4j3a4/

var center = ol.proj.transform([4.90756, 45.5172], 'EPSG:4326', 'EPSG:3857');
var map = new ol.Map({
    layers: [new ol.layer.Tile({source: new ol.source.OSM()})],
    target: 'map',
    controls: ol.control.defaults({attributionOptions:  ({ collapsible: false})}),
    view: new ol.View({center: center,zoom: 12})
});
$('#layerOnTop').on("touchmove", function(event) {
    var scale = event.originalEvent.touches;
    if (scale.length==2) {
        event.preventDefault();
        var bottomEvent = new $.Event("touchmove");
        bottomEvent.pageX = event.pageX;
        bottomEvent.pageY = event.pageY;
        $("#map").trigger(bottomEvent);
      }
});
$('#map').on("touchmove", function(event) {console.log('ok')})

I did my test with an Android phone.

Somebody have an idea, please ?


Solution

  • I finally found :) There was 2 errors in my code : 1) target should be the canvas element in div#map and not #map himself 2) I fixed the touch identifier by Date.now() and add +1 for the second, it was certainely too big

    See below the code if one day somebody search ...

    $('#layerOnTop').on("touchstart touchmove touchend", function(event) {
      view = document.getElementById('map').ownerDocument.defaultView;
      if (event.originalEvent.touches.length==2) {
        var target = document.getElementById('map').getElementsByTagName('canvas')[0];
        var pageX1 = event.originalEvent.touches[0].pageX;
        var pageY1 = event.originalEvent.touches[0].pageY;
        var pageX2 = event.originalEvent.touches[1].pageX;
        var pageY2 = event.originalEvent.touches[1].pageY;
    
        var touch1 = document.createTouch(view, target, 0, pageX1, pageY1, 0, 0);
        var touch2 = document.createTouch(view, target, 1, pageX2, pageY2, 0, 0);
        var touchList = document.createTouchList(touch1, touch2);
        var touchEvent = new TouchEvent(event.type, {cancelable: true, bubbles: true, touches: touchList, targetTouches: touchList, changedTouches: touchList})
        target.dispatchEvent(touchEvent);
      }
    });
    

    http://jsfiddle.net/2ek4j3a4/7/