gpt4 book ai didi

javascript - Google map - 如何在不刷新布局的情况下添加标记

转载 作者:行者123 更新时间:2023-12-02 16:14:37 24 4
gpt4 key购买 nike

添加标记时遇到问题。

        var marker = new google.maps.Marker({
position: myLatlng,
title:"Hello World!"
});

// To add the marker to the map, call setMap();
marker.setMap(map);

https://jsfiddle.net/73ogq84a/

简单的例子,每一秒我都会放置标记,页面不会重新加载,但会重新加载一些布局,我们会看到 map 重新加载的效果。

可以顺利地放置标记。

但是在这个页面http://www.flightradar24.com/simple_index.php一切正常,飞机正在飞行, map 重新加载没有影响。

最佳答案

使标记和 map 全局化。不要每次都重新创建 map ,只需移动标记即可。

var map, marker, myLatlng;

setInterval(function () {
if (!marker || !marker.setPosition) {
marker = new google.maps.Marker({
position: myLatlng,
title: "Hello World!"
});

// To add the marker to the map, call setMap();
marker.setMap(map);
} else {
marker.setPosition(myLatlng);
}

}, 5000);

updated fiddle

关于javascript - Google map - 如何在不刷新布局的情况下添加标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29806815/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com