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

时间:2013-11-14 06:35:00

标签: google-maps

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

> 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;


    } 

1 个答案:

答案 0 :(得分:3)

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

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

取而代之的是markers.setMap(null);。 此外,我不确定你的markers是什么,它是Google标记的正确集合。 这个集合应该是这样的:

markers[i] = new google.maps.Marker({
        position: new google.maps.LatLng('some latitude', 'some longitude'),
        map: map, //object of Google Map
        name: 'Marker Name'
    });
相关问题