【问题标题】:Rotate SVG at the center for Google maps custom icon在 Google 地图自定义图标的中心旋转 SVG
【发布时间】:2018-02-24 01:57:08
【问题描述】:

我有在谷歌地图上绘制旋转图标的代码:

    function createMarker(device) {
        var wtf = new google.maps.Marker({
        map: window.map_,
        position: new google.maps.LatLng(device.lat, device.lng),
        icon: {
            path: 'M350,0 700,700 350,550 0,700',
            fillColor: "limegreen",
            fillOpacity: 0.8,
            scale: 0.04,
            strokeColor: 'limegreen',
            strokeWeight: 1,
            anchor: new google.maps.Point(800, 800),
            rotation: device.head,
        }
      });
      return wtf;
}

问题是旋转在 SVG 的一个角上旋转 - 而不是中心。我在某处找到了 svg 路径,通过反复试验对其进行了缩放,并猜测了锚点。当我添加标签时,标签不是在图标下,而是全部搞砸了。标签使用与标记相同的纬度/经度。

参见示例:

我发现无法让图标在标签上方“当场”旋转。关于如何让它发挥作用的任何想法?谢谢

【问题讨论】:

  • 您找到解决方案了吗
  • 如果您找到解决方案有任何更新吗?

标签: svg google-maps-markers


【解决方案1】:

这并不理想,但可以正常工作。这是 TypeScript 代码。用 TypeScript 写完就不能再回到 JavaScript 了,太痛苦了。

鉴于 pos 是一个 LatLng:

let pos = new this.google.maps.LatLng(lat, lng);

方法:

public createMarker(heading: number, pos: any) {
    let marky = new this.google.maps.Marker({
        position: pos,
        map: this.google_map,
        icon: {
            path: this.google.maps.SymbolPath.FORWARD_CLOSED_ARROW,
            fillOpacity: 1,
            fillColor: 'orange',
            strokeWeight: 1.5,
            scale: 2,
            strokeColor: 'darkblue',
            rotation: heading,
            anchor: this.getRotation(heading),
        },
    });
    return marky;
}

public createLabel(name: string, pos: any) {
    let label = new MapLabel({
        text: name,
        position: pos,
        map: this.google_map,
        fontSize: 17,
        fontColor: 'darkred',
        align: 'center'
    });
    return label;
}

public getRotation(angle: number) {
    if (angle <= 30) return new this.google.maps.Point(0, 5);
    if (angle <= 45) return new this.google.maps.Point(0, 5);
    if (angle <= 60) return new this.google.maps.Point(2, 4);
    if (angle <= 120) return new this.google.maps.Point(3, 0);
    if (angle <= 150) return new this.google.maps.Point(0, 0);
    if (angle <= 210) return new this.google.maps.Point(0, 0);
    if (angle <= 220) return new this.google.maps.Point(-1, 0);
    if (angle <= 240) return new this.google.maps.Point(-3, 0);
    if (angle <= 280) return new this.google.maps.Point(-3, -3);
    return new this.google.maps.Point(0, 5);
}

【讨论】:

  • 通过反复试验完成。应该有更好的办法。
猜你喜欢
  • 2023-02-09
  • 1970-01-01
  • 1970-01-01
  • 2018-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-09
  • 2021-08-08
相关资源
最近更新 更多