从searchBar函数中删除/重置所有边界/标记

时间:2014-10-21 08:39:06

标签: javascript jquery google-maps google-maps-api-3

我使用了我在GoogleMaps文档中找到的searchBar函数,它的功能就像魅力一样,但我之所以使用它是因为我希望用户能够将GoogleMap捕获为图像(我知道如何使用HTML2Canvas)

我遇到的问题是,当我出于某种原因使用SearchBar功能时,它似乎能够突破界限"坚持"到使用SearchBar时应用的边界,所以当我尝试渲染Google Map的画布时,渲染的视口保持与使用SearchBox时相同,但实际渲染的图像位于相对于SearchBox视口(你可以在下面的图片中看到我的意思)。

SearchBar使用后的图片:

enter image description here

在SearchBar视口附近渲染时的图像(此图像是在我是PostCode标记的NW时渲染的,带有图像的区域的右下角是开始渲染时可见区域的右下角):

enter image description here

如上所示,图像的实际视口在每个视口中都是相同的。这让我相信在下面显示的SearchBox代码中设置的边界存在某种形式的问题:

JavaScript的:

var map;
var markers = [];
function initialize() {
    var mapOptions = {
        center: { lat: 54.559322, lng: -4.174804},
        zoom: 6,
        mapTypeId: google.maps.MapTypeId.HYBRID,
        panControl: true,
        mapTypeControl: false,
        scaleControl: true
        };
    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

    var input = (document.getElementById('pac-input'));
    map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
    var searchBox = new google.maps.places.SearchBox(input);

    google.maps.event.addListener(searchBox, 'places_changed', function() {
        var places = searchBox.getPlaces();

        if (places.length == 0) {
            return;
        }
        for (var i = 0, marker; marker = markers[i]; i++) {
            marker.setMap(null);
        }

        markers = [];
        var bounds = new google.maps.LatLngBounds();
        for (var i = 0, place; place = places[i]; i++) {
            var image = {
                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 marker = new google.maps.Marker({
                map: map,
                icon: image,
                title: place.name,
                position: place.geometry.location
            });

            markers.push(marker);

            bounds.extend(place.geometry.location);
        }

        map.fitBounds(bounds);
    });

}

我认为需要做的是在运行渲染时删除/重置SearchBox函数中的绑定信息集,但是您可以提供的任何信息都将非常感谢!

此致

克里斯

0 个答案:

没有答案