0

I would like to apply a specific style to a google.maps.GroundOverlay to rotate it.

function (imageUrl: string, bounds: google.maps.LatLngBounds, map: google.maps.Map){
  let overlay = new google.maps.GroundOverlay(
    imageUrl,
    bounds);
  overlay.setMap(map);
  // looking for something like overlay.style.rotate = '45deg'
}

How to add a style to google.maps.GroundOverlay in Javascript?

Thanks


Edit 1:

So I was thinking to find the element in the dom like:

[...]
overlay.setMap(map);
console.log(document.querySelectorAll("img[src='" + imageUrl + "']"));

but curiously it returns an empty array even if when I look in the source of the browser and I m able to find the image...

I continue to search

MrUpsidown
  • 21,592
  • 15
  • 77
  • 131
Cedric Arnould
  • 1,991
  • 4
  • 29
  • 49
  • 1
    Duplicate of [Google map: How to rotate \`groundoverlay\` need trick](https://stackoverflow.com/questions/52948764/google-map-how-to-rotate-groundoverlay-need-trick) – MrUpsidown Jul 11 '20 at 20:51

1 Answers1

0

From my understanding, I had no other choice than creating a custom Overlay. Here is my solution mostly inspirered from This Answer

rotated-overlay.ts

export class CustomOverlay extends google.maps.OverlayView {
  private div;

  constructor(
    private bounds: google.maps.LatLngBounds,
    private image: string,
    private rotation: number
  ) {
    super();

    // Define a property to hold the image's div. We'll
    // actually create this div upon receipt of the onAdd()
    // method so we'll leave it null for now.
    this.div = null;
  }

  /**
   * onAdd is called when the map's panes are ready and the overlay has been
   * added to the map.
   */
  onAdd() {
    const div = document.createElement('div');
    div.style.borderStyle = 'none';
    div.style.borderWidth = '0px';
    div.style.position = 'absolute';

    // Create the img element and attach it to the div.
    const img = document.createElement('img');
    img.src = this.image;
    img.style.width = '100%';
    img.style.height = '100%';
    img.style.position = 'absolute';
    div.appendChild(img);

    this.div = div;

    // Add the element to the "overlayLayer" pane.
    const panes = this.getPanes();
    panes.overlayLayer.appendChild(div);
  };

  draw() {

    // We use the south-west and north-east
    // coordinates of the overlay to peg it to the correct position and size.
    // To do this, we need to retrieve the projection from the overlay.
    const overlayProjection = this.getProjection();

    // Retrieve the south-west and north-east coordinates of this overlay
    // in LatLngs and convert them to pixel coordinates.
    // We'll use these coordinates to resize the div.
    const sw = overlayProjection.fromLatLngToDivPixel(this.bounds.getSouthWest());
    const ne = overlayProjection.fromLatLngToDivPixel(this.bounds.getNorthEast());

    // Resize the image's div to fit the indicated dimensions.
    const div = this.div;
    div.style.left = sw.x + 'px';
    div.style.top = ne.y + 'px';
    div.style.width = (ne.x - sw.x) + 'px';
    div.style.height = (sw.y - ne.y) + 'px';
    div.style.transform = 'rotate(' + this.rotation + 'deg)';
  };

  // The onRemove() method will be called automatically from the API if
  // we ever set the overlay's map property to 'null'.
  onRemove() {
    this.div.parentNode.removeChild(this.div);
    this.div = null;
  };
};

map-component.ts

public addOverlay(imageUrl: string, rotation: number, bounds: google.maps.LatLngBounds, map: google.maps.Map){
  let overlay = new CustomOverlay(
    bounds,
    imageUrl,
    rotation,
    
  );
  overlay.setMap(map);
}
Cedric Arnould
  • 1,991
  • 4
  • 29
  • 49