You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Google Maps AdvancedMarkerView标记的位置?

修改AdvancedMarkerView标记位置的方法

AdvancedMarkerView和传统Google Maps Marker的API设计不同,它并不支持setPosition()方法,想要更新标记位置,直接修改实例的position属性即可。

具体实现代码

假设你已经有了新的目标位置newLocation(可以是google.maps.LatLng对象,或者包含lat和lng属性的字面量对象),直接赋值给标记的position属性:

// 方式1:使用LatLng对象
centerMarker.position = new google.maps.LatLng(39.9042, 116.4074);

// 方式2:使用LatLngLiteral字面量
centerMarker.position = { lat: 39.9042, lng: 116.4074 };

结合你的代码示例

如果要在现有代码基础上添加位置更新逻辑,比如在某个事件触发时更新位置:

// 原创建标记的代码
const centerMarkerE = document.createElement("div");
let centerMarkerHtml = "<div class='center_marker'><img src='/images/front/pin.png' /></div>";
centerMarkerE.innerHTML = centerMarkerHtml;

let centerMarker = new google.maps.marker.AdvancedMarkerView({
  map,
  position: initialLocation,
  content: centerMarkerE,
});

// 后续需要更新位置时执行
const newLocation = { lat: 31.2304, lng: 121.4737 }; // 示例位置:上海
centerMarker.position = newLocation;

内容的提问来源于stack exchange,提问作者Ryan Wong

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 20:12:07