2011-11-28 52 views
1

我試圖爲我的主頁做一些事件監聽器。但我只能註冊一個事件。適合我的事件是添加Marker和一個圓圈。但是當我嘗試做其他事情時,比如在標記上按下來刪除它,或者在改變半徑時從圓上取下半徑。 任何人都知道我做錯了什麼?我在展示地圖時也遇到了問題。地圖得到加載,我可以看到除地圖以外的所有內容。Google maps javascript eventproblem

var map = new google.maps.Map(document.getElementById("map_areas"), { 
    zoom:8, 
    center: latlng, 
    mapTypeId:google.maps.MapTypeId.ROADMAP, 
    streetViewControl: false, 
    zoomControl: true, 
    zoomControlOptions: { style: google.maps.ZoomControlStyle.LARGE}, 
    overviewMapControl: true, 
    panControl: true 
}); 

var marker; 
var circle; 
google.maps.event.addListener(map, 'click', function(evt) { 
    addMarkerAndCircle(evt); 
}); 

function addMarkerAndCircle(evt) { 
    marker = new google.maps.Marker({ 
     position: evt.latLng, 
     animations: google.maps.Animation.DROP, 
     map:map, 
     draggable: true, 
     clickable:true 
    }); 
    circle = new google.maps.Circle({ 
     map:map, 
     radius:16093, // 10 miles in metres 
     fillColor:'#AA0000', 
     clickable: true, 
     editable: true 
    }); 
    circle.bindTo('center', marker, 'position'); 
    markersArray.push(marker); 
    circleArray.push(circle); 
} 

google.maps.event.addListener(circle, 'radius_changed', function() { 
    var radius = circle.getRadius(); 
}); 

http://img231.imageshack.us/img231/6739/googlemapsv.jpg

+0

我已經看到了地圖部分負載的問題,當我發起了一個地圖內一個隱藏的div,稍後會顯示。那是你在做什麼?在這種情況下,是否可以在顯示div的位置啓動地圖? –

+0

這條線是否工作?想必你還是想用半徑做些什麼...... var radius = circle.getRadius(); – duncan

+0

我認爲該行的作品:)。但事件.addlistener劑量。我想從圓圈中獲取半徑並保存。 – SolidALb

回答

4

你需要的圓圈事件偵聽器附加到圓創建時。在您的代碼中,您將偵聽器添加到尚未定義的圓中。試試這個:

function addMarkerAndCircle(evt) { 
    marker = new google.maps.Marker({ 
     position: evt.latLng, 
     animations: google.maps.Animation.DROP, 
     map:map, 
     draggable: true, 
     clickable:true 
    }); 
    circle = new google.maps.Circle({ 
     map:map, 
     radius:16093, // 10 miles in metres 
     fillColor:'#AA0000', 
     clickable: true, 
     editable: true 
    }); 
    circle.bindTo('center', marker, 'position'); 
    addRadiusChanged(circle); 
    markersArray.push(marker); 
    circleArray.push(circle); 
} 

function addRadiusChanged(c) { 
    google.maps.event.addListener(c, 'radius_changed', function() { 
     var radius = c.getRadius(); 
     // do something with radius 
    }); 

你可以在地圖來調整自己,以填補這個DIV:

google.maps.event.trigger(map,'resize'); 
+0

thx很多:D!它像一個魅力一樣工作 – SolidALb

+0

你能接受答案嗎? –

+0

是的,當然認爲它完成了 – SolidALb