在javascript中访问谷歌地图对象实例,以在页面加载后更改地图选项?

时间:2015-10-01 01:31:56

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

我有一个由后端服务动态生成的地图,该服务生成以下javascript。在不修改下面的代码的情况下,我有什么方法可以访问在< initMap'中创建的google.maps.Map()实例。下面的函数,以便我可以在页面加载后更改地图选项?

var googleMap_SearchResultsGoogleMapV31 = {
    addresses: [],
    initMap: function() {
        var bounds = new google.maps.LatLngBounds,
            mapOptions = {
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                center: new google.maps.LatLng(-37.8136, 144.9631)
            },
            map = new google.maps.Map(document.getElementById("page_layout_page_template_SearchResultsGoogleMapV31_map-canvas"), mapOptions);
        for (i = 0; i < googleMap_SearchResultsGoogleMapV31.addresses.length; i++) googleMap_SearchResultsGoogleMapV31.addAddressMarker(map, googleMap_SearchResultsGoogleMapV31.addresses[i], bounds, 300);
        setTimeout(function() {
            if (googleMap_SearchResultsGoogleMapV31.addresses.length > 1) {
                map.fitBounds(bounds);
                map.panToBounds(bounds)
            } else map.setZoom(4)
        }, 1500)
    },
    addAddressMarker: function(map, addressToLocate, markerBounds, infoMaxWidth) {
        var geocoder = new google.maps.Geocoder;
        geocoder.geocode({
            address: addressToLocate.address
        }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                googleMap_SearchResultsGoogleMapV31.addMarker(map, results[0].geometry.location, addressToLocate.title, markerBounds, addressToLocate.info, infoMaxWidth);
                googleMap_SearchResultsGoogleMapV31.addresses.length == 1 && map.setCenter(results[0].geometry.location)
            }
        })
    },
    addMarker: function(map, location, name, markerBounds, infoContent, infoMaxWidth) {
        markerBounds.extend(location);
        var marker = new google.maps.Marker({
            position: location,
            map: map,
            title: name,
            status: "active"
        });
        if (infoContent && infoContent != "") {
            var markerInfo = new google.maps.InfoWindow({
                content: "<div id='infoContent' class='mapInfoContent'>" + infoContent + "</div>",
                maxWidth: infoMaxWidth
            });
            google.maps.event.addDomListener(marker, "click", function() {
                markerInfo.open(map, marker)
            })
        }
    }
};
google.maps.event.addDomListener(window, "load", googleMap_SearchResultsGoogleMapV31.initMap)

0 个答案:

没有答案