2012-09-05 45 views
9

Tôi đang cố gắng để làm cho nó có thể thay đổi nội dung hiển thị bên trong một DIV đó là nội dung của một infowindow. Tôi đã có thể thay đổi nội dung từ Hello thành YO bên trong infowindow. Vấn đề là khi tôi đóng infowindow và mở lại nó, nội dung cập nhật sẽ trở lại bản gốc. Dưới đây là mã của tôi:thay đổi nội dung infowindow bản đồ v3

google.maps.event.addListener(drawingManager, 'overlaycomplete', function(event) { 
     if (event.type == google.maps.drawing.OverlayType.MARKER) { 
      //event.overlay.setTitle("Hello"); 
      var infowindow = new google.maps.InfoWindow({ 
       content: '<div id="content" onmouseover="updateContent()">Hello</div>', 
       maxWidth: 10 
      }); 
      google.maps.event.addListener(event.overlay, 'click', function() { 
       infowindow.open(map, event.overlay); 
      }); 
     } 
    }); 

    function updateContent() { 
     document.getElementById('content').innerHTML = "Yo"; 
    } 

tôi về cơ bản muốn tạo ra một cửa sổ thông tin mặc định và cho phép người dùng nhập văn bản riêng của họ sau khi họ đặt marke trên trang ...

Trả lời

5

bạn phải thiết lập nội dung thông qua phương pháp setContentHTML

var infowindow ; 
google.maps.event.addListener(drawingManager, 'overlaycomplete', function(event) { 
    if (event.type == google.maps.drawing.OverlayType.MARKER) { 
     //event.overlay.setTitle("Hello"); 
     infowindow = new google.maps.InfoWindow({ 
      content: '<div id="content" onmouseover="updateContent()">Hello</div>', 
      maxWidth: 10 
     }); 
     google.maps.event.addListener(event.overlay,'click',function() 
     infowindow.open(map,event.overlay); 
    }); 
}}); 

function updateContent(){ 
    infowindow.setContent("YO"); 
} 
+0

Khi tôi đã thử điều này tôi nhận được một lỗi rằng setContentHTML không phải là một phương thức của đối tượng infoWindow của Google (tôi tin rằng đó là tiêu chuẩn DHTML nhưng đối tượng infoWindow của Google không kế thừa nó). Để đạt được mục tiêu của OP, tôi đã tách ra thông tin hiện có, tạo một đối tượng infoWindow mới với nội dung mới sau đó đính kèm nó vào điểm đánh dấu hiện có. –

+7

Thay vào đó, bạn có thể sử dụng chức năng "infowindow.setContent (...)", hoạt động tốt cho tôi :) – ghedas

+2

Định danh đẹp –

12

tôi tìm thấy câu trả lời được chấp nhận ở trên đã không làm việc như tôi đã phải sử dụng setContent() như sau:

google.maps.event.addListener(drawingManager, 'overlaycomplete', function (event) { 
    if (event.type == google.maps.drawing.OverlayType.MARKER) { 
     //event.overlay.setTitle("Hello"); 
     var infowindow = new google.maps.InfoWindow({ 
      content: '<div id="content" onmouseover="updateContent()">Hello</div>', 
      maxWidth: 10 
     }); 
     google.maps.event.addListener(event.overlay, 'click', function() { 
      infowindow.open(map, event.overlay); 
     }); 
    } 
}); 
function updateContent() { 
    infowindow.setContent("Yo"); 
} 
Các vấn đề liên quan