自从 Marker 在 Google 地图中被弃用以来,我们一直致力于转向 AdvancedMarker。 我正在尝试迁移标记,但他们将其渲染得如此复杂,毫无意义......
我曾经有一个简单的 svg 标记
new google.maps.Marker({
position:loc,
map:map,
title: title,
icon: '/img/marker.svg'
});
现在迁移后,我一辈子都无法弄清楚如何使用相同的路径来处理绝对混乱的 PinElement。
const pinElement = new google.maps.marker.PinElement({
glyph: new URL('/img/marker.svg'),
});
new google.maps.marker.AdvancedMarkerElement({
position: loc,
map: map,
title: title,
content: pinElement
});
Failed to construct 'URL': Invalid URL
如何加载该 URL WITHOUT 在字形中指定域?我们在多个环境中使用它,因此域可以更改,这就是我希望使用绝对 URL 的原因。
我会回答我自己的问题,我可能会帮助别人。在浏览完充满拼写错误和错误的文档后,我在随机页面上找到了该解决方案:
let img = document.createElement("img");
img.src ='/img/marker.svg';
new google.maps.marker.AdvancedMarkerElement({
position: loc,
map: map,
title: title,
content: img
});