当前位置: 首页 > 知识库问答 >
问题:

谷歌地图标记在添加新标记时未被清除

厉熠彤
2023-03-14

我有一个函数,以一个地名作为输入,并在谷歌地图上那个地方的lat和lng位置下降一个pin。它还使用lat和lng位置设置边界,将pin设置为Viewport。一切都很好,但旧的标记没有得到清除,而添加一个新的标记。我已经清除了标记数组,但它不起作用。这是我的代码

  var place = args.address;

        var bounds = new google.maps.LatLngBounds();

        var icon = {
            url: place.icon,
            size: new google.maps.Size(71, 71),
            origin: new google.maps.Point(0, 0),
            anchor: new google.maps.Point(17, 34),
            scaledSize: new google.maps.Size(25, 25)
        };

      var geocoder = new google.maps.Geocoder();
        geocoder.geocode({
            'address': place.formatted_address
        }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                console.log(results);

                var markers = [];

                 markers.forEach(function(marker) {
                marker.setMap(null);
            });

               markers.push(new google.maps.Marker({
                    map: map.map,
                    icon: icon,
                    title: place.name,
                    position: results[0].geometry.location
                }));


                if (results[0].geometry.viewport) {
                    bounds.union(results[0].geometry.viewport);
                } else {
                    bounds.extend(results[0].geometry.location);
                }

                map.map.fitBounds(bounds);

            } else {
                console.log("error")
            }
        });

共有1个答案

柴宏浚
2023-03-14

试试这个

        var place = args.address;

        var bounds = new google.maps.LatLngBounds();

        var markers = []; //

        var icon = {
            url: place.icon,
            size: new google.maps.Size(71, 71),
            origin: new google.maps.Point(0, 0),
            anchor: new google.maps.Point(17, 34),
            scaledSize: new google.maps.Size(25, 25)
        };

      var geocoder = new google.maps.Geocoder();
        geocoder.geocode({
            'address': place.formatted_address
        }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                console.log(results);                 
                markers.forEach(function(marker) {
                marker.setMap(null);

            });
               markers = []; // Empty here
               markers.push(new google.maps.Marker({
                    map: map.map,
                    icon: icon,
                    title: place.name,
                    position: results[0].geometry.location
                }));


                if (results[0].geometry.viewport) {
                    bounds.union(results[0].geometry.viewport);
                } else {
                    bounds.extend(results[0].geometry.location);
                }

                map.map.fitBounds(bounds);

            } else {
                console.log("error")
            }
        });
javascript google-maps goog
 类似资料:
  • 我想将事件侦听器添加到所有标记中,我正在使用GoogleMapsforRailsGem。这是我的密码 我试图将事件侦听器添加到所有标记中,但它没有发出任何警报。如何在google maps for rails中添加事件侦听器 我有一个数据数组,每个数据包含id,lat,lng

  • 我已经尝试在这里和谷歌地图API留档上查看了大量的代码块,但仍然无法找出如何隐藏标记。 这是我正在使用的当前代码,它在一个实例中有效。有一次,我在带有标记的函数中添加了“for”循环。setMap(null)Firefox显示以下错误: markers.set地图不是一个函数 附加信息:校园地图和完整代码(不包括地图API)

  • 所以我正在准备一个页面,其中有一个谷歌地图,有两个形式,即纬度和经度,和一个提交按钮。

  • 我知道周围也有类似的问题,但我无法找到一个可靠的答案,这是我的问题:有没有什么方法可以在没有谷歌地图引用的情况下将标记存储在ArrayList(或任何其他存储)中,然后简单地将它们添加到我的地图中? 背景:我有一个应用程序,目前有大约3500个标记。每个标记还有一个与之相关联的数据(布尔数组存储每个标记的数据,用于根据用户交互使它们可见/不可见)。目前,我使用扩展AsyncTask的类来获取这些标

  • 我想更改Google Maps上选定的标记图标,因此我有以下代码: 在这一行,我从下面得到错误: 例外情况如下:

  • 我使用Google Maps Android SDK在地图上添加标记,这些标记的位置存储在我的应用程序的所有用户都可以使用的Firebase数据库中。每个标记的数据存储在唯一的Firebase记录中,每个记录包含纬度 添加了一个新标记。 更改现有标记(移动到新位置和/或“验证”) 删除现有标记 我实现了一个哈希图,如如何使用Firebase Google Maps API Android中的数据更