[英]Problems calling fitbounds outside jquery.each()
我正在用 JavaScript(v3) 開發谷歌地圖。 我需要從 XML 中顯示一些標記,為此我使用了 jQuery。
這是對象和函數,可能會節省我解釋的時間:
var VX = {
map:null,
bounds:null
}
VX.placeMarkers = function(filename) {
$.get(filename, function(xml) {
$(xml).find("marker").each(function() {
var lat = $(this).find('lat').text();
var lng = $(this).find('lng').text();
var point = new google.maps.LatLng(parseFloat(lat),parseFloat(lng));
VX.bounds.extend(point);
VX.map.fitBounds(VX.bounds); //this works
var marker = new google.maps.Marker({
position: point,
map: VX.map,
zoom: 10,
center: point
});
});
});
//VX.map.fitBounds(VX.bounds); //this shows me the ocean east of Africa
}
所以基本上我的問題是我無法弄清楚如何從 .each 函數外部進行擬合,而在函數內部執行此操作會為每個看起來不好的標記調用它。
我在初始化地圖時聲明了邊界......沒有包含整個代碼,因為它像 300 行。
我不應該能夠使用我傳遞給全局對象的值嗎?
編輯:啊,我是從 get 函數外部調用它的!
第二個調用不起作用,因為它在 ajax get()
返回之前觸發。
將fitBounds
放在get()
處理程序中,但在each()
函數之外。 像這樣:
var VX = {
map:null,
bounds:null
}
VX.placeMarkers = function(filename)
{
$.get
(
filename,
function(xml)
{
$(xml).find("marker").each
(
function()
{
var lat = $(this).find('lat').text();
var lng = $(this).find('lng').text();
var point = new google.maps.LatLng(parseFloat(lat),parseFloat(lng));
VX.bounds.extend(point);
//VX.map.fitBounds(VX.bounds); //this works
var marker = new google.maps.Marker
({
position: point,
map: VX.map,
zoom: 10,
center: point
});
}
);
VX.map.fitBounds(VX.bounds); //-- This should work.
}
);
//VX.map.fitBounds(VX.bounds); //this shows me the ocean east of africa
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.