【问题标题】:How to apply a custom css to a google.maps.GroundOverlay in Javascript? [duplicate]如何在 Javascript 中将自定义 css 应用于 google.maps.GroundOverlay? [复制]
【发布时间】:2020-07-10 23:20:01
【问题描述】:

我想将特定样式应用于 google.maps.GroundOverlay 以对其进行旋转。

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'
}

如何在 Javascript 中为 google.maps.GroundOverlay 添加样式?

谢谢


编辑 1:

所以我想在 dom 中找到这样的元素:

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

但奇怪的是,即使当我查看浏览器的源代码并且我能够找到图像时,它也会返回一个空数组...

我继续搜索

【问题讨论】:

标签: javascript google-maps google-maps-api-3


【解决方案1】:

据我了解,除了创建自定义叠加层之外,我别无选择。这是我的解决方案,主要灵感来自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;
  };
};

地图组件.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);
}

【讨论】:

    猜你喜欢
    • 2011-05-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-29
    • 2018-05-12
    • 2019-06-12
    • 2018-08-02
    • 2015-06-27
    • 2012-06-13
    相关资源
    最近更新 更多