google-maps - 无法在谷歌地图中使用 setMap(null) 或 setVisible(false) 清除标记

标签 google-maps

我在谷歌地图上使用了淘汰赛 javascrit,但我无法清除 map 上的标记,我在那里收到错误消息,错误如下

> Ripple :: Environment Warming Up (Tea. Earl Gray. Hot.) ripple.js:37
> web :: Initialization Finished (Make it so.) ripple.js:37 Uncaught
> TypeError: Object [object Object] has no method 'dxmap' Shops.js:70
> eula response:  true ripple.js:47 Uncaught TypeError: Object [object
> Object] has no method 'dxmap' Shops.js:70 Uncaught TypeError: Object
> #<Object> has no method 'setMap' Shops.js:137

我下面的代码你能帮帮我吗
function FetchNearestShops(latitud,longitud)
    {
        $.ajax({
            type: 'POST',
            async:false,
            contentType: 'application/json',
            dataType: 'jsonp',
            url: url + "/GetNearestShop",
            //data: { Latitude: 9.998472, Longitude: 76.308059 },
            data: { Latitude: latitud, Longitude: longitud },
            success: function (data) {                
                loc.removeAll();
                productsNear.removeAll();
                $.map(data, function (item) {
                    loc.push({ label: item.ShopName, location: [item.Latitude, item.Longitude] });
                    //productsNear.push({ Name: item.ShopName, IsFavorite: false, Address: "", id: item.ShopId, Image: "mockcontent/shopimages/1.jpg" });
                    productsNear.push({ Name: item.ShopName, IsFavorite: item.Isfavourite, Address: item.Address, id: item.ShopId, Image: item.ImageUrl });
                });
                alert();
                viewModel.options.markers = loc;

            },
            error: function () {
                alert("error");
            }

        });
    }
    function SetLocation() {
        var geocoder = new google.maps.Geocoder();
        //var address = "ernakulam";
        //alert(viewModel.options.location());
        geocoder.geocode({ 'address': txtAddress() }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var latitude = results[0].geometry.location.lat();
                var longitude = results[0].geometry.location.lng();
                ClearMap();

                FetchNearestShops(latitude, longitude);
            }
        });
    }
    function ClearMap()
    {
        var markers = viewModel.options.markers();
        for (var i = 0; i < markers.length; i++) {
            markers[i];
            markers.setMap(null);
        }
        markers;


    } 

最佳答案

我认为您的 ClearMap() 的循环有问题功能。尝试

for (var i = 0; i < markers.length; i++) {
    markers[i].setMap(null);
}

而是 markers.setMap(null); .
我也不确定你的markers它是谷歌标记的正确集合。
此集合应如下所示:
markers[i] = new google.maps.Marker({
        position: new google.maps.LatLng('some latitude', 'some longitude'),
        map: map, //object of Google Map
        name: 'Marker Name'
    });

关于google-maps - 无法在谷歌地图中使用 setMap(null) 或 setVisible(false) 清除标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19971157/

相关文章:

javascript - Google map 将信息框标记在完全相同的位置

ruby-on-rails - 谷歌地图信息窗口缺少图像/角落

javascript - 谷歌地图回调后Angular2双向绑定(bind)停止工作

javascript - 将样式应用于融合表上的图标时,得到意外的标识符

android - Google Maps API v2 法律声明字符串太长

javascript - 漂亮的谷歌地图实现黑白

google-maps - Google Maps API V3自定义控件大小

java - Android Manifest 未向 Google map 提供 API key

Javascript google map api addDomListener 更改此上下文

google-maps - v2 到 v3 Google Maps 包装器 API 来测试旧应用程序