Google地图:如何旋转`groundoverlay`需要技巧

时间:2018-10-23 12:10:04

标签: jquery css google-maps

我正在制作地图项目,例如在地图上添加形状和位置并将其保存。

但是我在地面覆盖方面遇到了问题。

我知道Google地图文档中不存在旋转属性。 但是我需要一种方法/技巧来通过其他任何方式旋转地面覆盖图像。

此处代码

var srcImage = "http://demo/image/uploads/demo.jpg";
var bounds = {
    north: 44.599,
    south: 44.490,
    east: -78.443,
    west: -78.649
}
var overlay = new google.maps.GroundOverlay(srcImage ,bounds);
overlay.setMap(map);

我正在使用滑块旋转叠加。

$("#overlayslider").slider().on('slide',function(e){
     var angle = e.newVal;
     // i want rotate overlay by angle/degree as per slider
})

我尝试了投影,但是没有成功。

任何可能的方式都会受到赞赏。

任何帮助都会有用,谢谢。

2 个答案:

答案 0 :(得分:0)

您可以通过使用自定义叠加层来实现此目的:https://developers.google.com/maps/documentation/javascript/examples/overlay-simple

将事件侦听器添加到USGSOverlay.prototype.onAdd中的叠加层滑块,然后在输入上设置div的旋转。

document.getElementById('overlayslider').addEventListener('input', function() {
    div.style.transform = 'rotate(' + this.value + 'deg)';
});

这是修改后的自定义覆盖示例:https://jsfiddle.net/b0tLd46u/4/。您可以通过地图上方的范围滑块设置叠加层的旋转。

答案 1 :(得分:0)

这是我的解决方案,主要受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);
}