2012-03-30 185 views
0

我是新来的stackoveflow,任何人可以帮助我如何使用谷歌API v3绘制矩形,我经历了一些谷歌示例我得到了下面的代码,如何使用谷歌地图API在谷歌地图上绘制矩形

function initialize() { 

    var coachella = new google.maps.LatLng(33.6803003, -116.173894); 
    var rectangle; 

    var myOptions = { 
    zoom: 11, 
    center: coachella, 
    mapTypeId: google.maps.MapTypeId.TERRAIN 
    }; 

    var map = new google.maps.Map(document.getElementById("map_canvas"), 
     myOptions); 

    rectangle = new google.maps.Rectangle(); 

    google.maps.event.addListener(map, 'zoom_changed', function() { 

    // Get the current bounds, which reflect the bounds before the zoom. 
    var rectOptions = { 
     strokeColor: "#FF0000", 
     strokeOpacity: 0.8, 
     strokeWeight: 2, 
     fillColor: "#FF0000", 
     fillOpacity: 0.35, 
     map: map, 
     bounds: map.getBounds() 
    }; 
    rectangle.setOptions(rectOptions); 
    }); 
} 

但是,它的变焦事件(变焦chanages),我想出来的事件简单的功能,请帮我

回答

2

map.getBounds()可能返回不期望的界限时,称为早期(立即在实例化地图之后)。

你可以使用tilesloaded代替

google.maps.event.addListenerOnce(map, 'tilesloaded', function() { 
    /*your code*/ 
    });