gpt4 book ai didi

javascript - 谷歌地图 API v3,getBound();不返回当前视口(viewport)的纬度/经度范围

转载 作者:行者123 更新时间:2023-11-30 18:11:43 24 4
gpt4 key购买 nike

我需要将我的混搭页面代码从 google map api v2 升级到 v3。但是 getBound() 不适用于新代码!错误在哪里?提前谢谢你。

旧代码是:

   <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;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&amp;v=2&amp;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>

最佳答案

目前 map 只能在初始化内部访问,使其全局可访问:

window.map = new google.maps.Map(/*.....*/);

此外:getBounds() 无法在 map 加载完成之前返回结果( map 加载是异步的,因此您不能立即调用 updateStatus())。

您可能会观察到 tilesloaded-event:

google.maps.event.addListenerOnce(map,'tilesloaded',updateStatus);

但是看起来你还想观察缩放和平移,你可以一次观察:

google.maps.event.addListener(map,'bounds_changed',updateStatus);

关于javascript - 谷歌地图 API v3,getBound();不返回当前视口(viewport)的纬度/经度范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14429943/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com