多个地理编码器请求google maps api v3

时间:2012-08-08 16:33:40

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

您好我一直在使用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);

1 个答案:

答案 0 :(得分:0)

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

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

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

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

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