0

嗨,我一直在使用 Google Maps API v3 和地理编码器来显示地图,到目前为止它运行良好。当我尝试在同一页面上显示另一张地图时,仅加载第一张地图。第二张地图只是显示了一个灰色区域。没有加载地图。我必须做多个地理编码器请求,但我该怎么做。到目前为止,这是我的代码:

    var geocoder;
var map;
var map2;
var marker;
var infobox;

function initialize() {
            //Geocoder
        geocoder = new google.maps.Geocoder();
        var address = 'Times Square, New York'; 
        geocoder.geocode( { 'address': address }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                map.setCenter(results[0].geometry.location);
            //Marker
            var companyLogo = new google.maps.MarkerImage('images/pin.png',
                new google.maps.Size(20,20),
                new google.maps.Point(0,0),
                new google.maps.Point(10,10)
                );
            //Marker shadow 
            var companyShadow = new google.maps.MarkerImage('images/pin_shadow.png',
                new google.maps.Size(25,33),
                new google.maps.Point(0,0),
                new google.maps.Point(0, 25)
                );
            //Display the marker
            var marker = new google.maps.Marker({
                map: map,
                icon: companyLogo,
                shadow: companyShadow,
                position: results[0].geometry.location,
                title:"Some title"
            });
            //Infobox
            infobox = new InfoBox({
                content: document.getElementById("infobox"),
                disableAutoPan: false,
                maxWidth: 320,
                pixelOffset: new google.maps.Size(-142, -150),
                zIndex: null,
                boxStyle: {
                    opacity: 1,
                    width: "300px"
                    },
                closeBoxMargin: "0px",
                closeBoxURL: "images/close_tooltip.png",
                infoBoxClearance: new google.maps.Size(1, 1)
                });
                        //Open infobox
            google.maps.event.addListener(marker, 'mouseover', function() {
                infobox.open(map, this);
                map.panTo(geocoder);
                });

            } else {
                alert("Geocode was not successful for the following reason: " + status);
                }
            });

                    //Map options
        var myOptions = {
            zoom: 15,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            disableDefaultUI: true,
            scrollwheel: false
        }
    //Call Maps 
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    map2 = new google.maps.Map(document.getElementById("map_canvas2"), myOptions);
}

google.maps.event.addDomListener(window, 'load', initialize);
4

1 回答 1

0

您用于设置第二张地图的myOptions对象不包含center属性,因此第二张地图无法居中。

调用地理编码器时,您设置第一张地图的中心:

map.setCenter(results[0].geometry.location);

但你没有设置第二张地图。

如果不设置地图的中心,它将保持灰色和空白。

于 2012-08-08T16:46:35.397 回答