由于对 google.maps.Marker 的支持将于 2024 年 2 月 21 日结束,我们正在考虑迁移到 google.maps.marker.AdvancedMarkerElement。 我用图像替换标记图标,但我不知道如何指定锚点。 在下面的 Google 示例中,beachflag.png 的锚点似乎位于左下角 (0, 32),但锚点显示在底部中心。 创建带有图形的标记
使用 google.maps.Marker,我可以指定锚点,如下所示,但是如何使用 AdvancedMarkerElement 指定锚点?
let icon = {
url: "https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png",
size: new google.maps.Size(20, 32),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(0, 32), //bottom left
};
let marker = new google.maps.Marker({
position: position,
map: map,
icon: icon
});
在问题跟踪器中查看此问题:https://issuetracker.google.com/issues/292069884
添加这个:
beachFlagMarkerView.content.style.transform = 'translateX(44%)'
对我有用(50%看起来不太正确)
beachFlagImg.src =
"https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png";
const beachFlagMarkerView = new AdvancedMarkerElement({
map,
position: { lat: 37.434, lng: -122.082 },
content: beachFlagImg,
title: "A marker using a custom PNG Image",
});
beachFlagMarkerView.content.style.transform = 'translateX(44%)
代码片段:
async function initMap() {
// Request needed libraries.
const { Map } = await google.maps.importLibrary("maps");
const { AdvancedMarkerElement, PinElement } = await google.maps.importLibrary(
"marker",
);
const { Place } = await google.maps.importLibrary("places");
const map = new Map(document.getElementById("map"), {
center: { lat: 37.434, lng: -122.082 },
zoom: 13,
mapId: "4504f8b37365c3d0",
});
const parser = new DOMParser();
// A marker with a with a URL pointing to a PNG.
var measle = new google.maps.Marker({
position: { lat: 37.434, lng: -122.082 },
map: map,
optimized: false,
icon: {
url: "https://maps.gstatic.com/intl/en_us/mapfiles/markers2/measle.png",
size: new google.maps.Size(7, 7),
anchor: new google.maps.Point(3.5, 3.5)
}
})
const beachFlagImg = document.createElement("img");
beachFlagImg.src =
"https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png";
const beachFlagMarkerView = new AdvancedMarkerElement({
map,
position: { lat: 37.434, lng: -122.082 },
content: beachFlagImg,
title: "A marker using a custom PNG Image",
});
beachFlagMarkerView.content.style.transform = 'translateX(44%)';
// A marker customized using a place icon and color, name, and geometry.
const place = new Place({
id: "ChIJN5Nz71W3j4ARhx5bwpTQEGg",
});
}
initMap();
/*
* Always set the map height explicitly to define the size of the div element
* that contains the map.
*/
#map {
height: 100%;
}
/*
* Optional: Makes the sample page fill the window.
*/
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
<!doctype html>
<!--
@license
Copyright 2019 Google LLC. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
-->
<html>
<head>
<title>Advanced Markers Using Graphics</title>
<script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
<script src="https://use.fontawesome.com/releases/v5.15.4/js/all.js"></script>
<!-- jsFiddle will insert css and js -->
</head>
<body>
<div id="map"></div>
<!-- prettier-ignore -->
<script>(g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})
({key: "AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk", v: "beta"});</script>
</body>
</html>