我需要将我的mashup页面的代码从google map api v2升级到v3。但是getBound()不能使用新代码!哪里出错了?谢谢你的推荐。
旧代码是:
<script src="http://maps.google.com/maps?file=api&v=2&key= (my key api)"
type="text/javascript"></script>
<script type="text/javascript">
function updateStatus() {
var div = document.getElementById('mapinfo');
div.innerHTML = map.getBounds();
document.forms[0].lat0.value = map.getBounds().getSouthWest().lat();
document.forms[0].lon0.value = map.getBounds().getSouthWest().lng();
document.forms[0].lat1.value = map.getBounds().getNorthEast().lat();
document.forms[0].lon1.value = map.getBounds().getNorthEast().lng();
get_pictures();
}
function onMapMove() {
//map.setCenter(map.getCenter());
updateStatus();
}
function onMapZoom(oldZoom, newZoom) {
//map.setCenter(map.getCenter(),newZoom)
updateStatus();
}
function load() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map"));
window.map = map
map.setCenter(new GLatLng(41.879786443521795,12.427597045898438), 6);
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
window.kh = new GKeyboardHandler(map);
GEvent.addListener(map,'moveend',onMapMove);
GEvent.addListener(map,'zoomend',onMapZoom);
updateStatus();
}
}
</script>
新代码:
<script src="http://maps.google.com/maps?file=api&v=2&key= (my api key)"
type="text/javascript"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?libraries=weather&sensor=true">
</script>
<script type="text/javascript">
function updateStatus() {
var div = document.getElementById('mapinfo');
div.innerHTML = map.getBounds();
document.forms[0].lat0.value = map.getBounds().getSouthWest().lat();
document.forms[0].lon0.value = map.getBounds().getSouthWest().lng();
document.forms[0].lat1.value = map.getBounds().getNorthEast().lat();
document.forms[0].lon1.value = map.getBounds().getNorthEast().lng();
get_pictures();
}
function onMapMove() {
//map.setCenter(map.getCenter());
updateStatus();
}
function onMapZoom(oldZoom, newZoom) {
//map.setCenter(map.getCenter(),newZoom)
updateStatus();
}
function load() {
if (GBrowserIsCompatible()) {
var mapOptions = {
zoom: 6,
center: new google.maps.LatLng(41.879786443521795,12.427597045898438),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
var weatherLayer = new google.maps.weather.WeatherLayer({
temperatureUnits: google.maps.weather.TemperatureUnit.CELSIUS
});
weatherLayer.setMap(map);
var cloudLayer = new google.maps.weather.CloudLayer();
cloudLayer.setMap(map);
updateStatus();
}
}
</script>
答案 0 :(得分:2)
目前只能在初始化内访问地图,使其可全局访问:
window.map = new google.maps.Map(/*.....*/);
此外:getBounds()在地图加载完成之前无法返回结果(地图异步加载,因此无法立即调用updateStatus())。
您可以观察tilesloaded-event:
google.maps.event.addListenerOnce(map,'tilesloaded',updateStatus);
但是,由于您似乎还想观察缩放和平移,您可以立即观察所有内容:
google.maps.event.addListener(map,'bounds_changed',updateStatus);
答案 1 :(得分:0)
您没有从getBounds()
调用返回任何内容的原因是因为您的map
变量不在全局范围内。尝试在您的函数之外声明您的map
变量并且它应该有效 - 因为您的getBounds()
声明没有任何问题。
就像......
var map; //declaring map variable globally
function updateStatus() {
//code..
}
function onMapMove() {
//code..
}
function onMapZoom(oldZoom, newZoom) {
//code..
}
function load() {
//using the global variable
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
}
答案 2 :(得分:0)
在触发bounds_changed事件之前,map.getBounds()不会返回有效值。您可能希望在bounds_changed。
的事件侦听器中运行updateStatus函数google.maps.event.addListener(map,'bounds_changed',updateStatus});