css-scaled google maps click incorrect coordinates

前端 未结 3 1976
一生所求
一生所求 2021-01-28 11:11

I have a Google Maps map inserted in a css-scaled container. Due to project specifics this cannot be avoided. I need to track clicks coords on this map, but being scaled map set

3条回答
  •  时光取名叫无心
    2021-01-28 11:20

    Ok, figured out how to correct fix (this is when transformation center is 0,0)

    function point2LatLng(point, transformScale, map) {
        var topRight = map.getProjection().fromLatLngToPoint(map.getBounds().getNorthEast());
        var bottomLeft = map.getProjection().fromLatLngToPoint(map.getBounds().getSouthWest());
        var scale = Math.pow(2, map.getZoom());
        var worldPoint = new google.maps.Point(point.x / transformScale / scale + bottomLeft.x, point.y / transformScale / scale + topRight.y);
        return map.getProjection().fromPointToLatLng(worldPoint);
      }
    
    gmaps.event.addListener(this.map, 'click', (event)=> {
              let transformMatrix = window.getComputedStyle(this.$el.closest('.container')).transform.match(/^matrix\((.+)\)$/)[1].split(', ');
              let transformScale = parseFloat(transformMatrix[0]);
              var marker = new gmaps.Marker({
                  position: point2LatLng(event.pixel, transformScale, this.map),
                  map: this.map
              });
            });
    

提交回复
热议问题