2010-02-17 63 views
0

我是JavaScript新手,也是谷歌地圖。我的任務是:如何在添加一次後添加另一個事件偵聽器..?

  • ,以允許用戶在地圖上添加標記
  • 顯示的信息窗口時,在信息窗口,如果標記標記
  • 顯示「吉隆坡城市中心」的用戶點擊指向吉隆坡市中心和其他地點的「未知地點」。

我的問題是我添加標記後,甚至不能顯示信息窗口。我可以在同一功能中添加新事件嗎?該代碼僅適用於添加標記。下面的代碼是後我從谷歌地圖編輯:

功能MyApplication的(){

this.counter = 0; 
this.map = new GMap2(document.getElementById("map_canvas")); 
    this.map.setCenter(new GLatLng(3.145423,101.696883), 13); 
    var myEventListener = GEvent.bind(this.map, "click", this, function(overlay, latlng) { 
    if (this.counter == 0) { 
     if (latlng) { 
     this.map.addOverlay(new GMarker(latlng)) 
     this.counter++; 
     } else if (overlay instanceof GMarker) { 
      //This code is never executed as the event listener is 
      //removed the second time this event is triggered 
     this.removeOverlay(marker) 
     } 
    } else { 
     GEvent.removeListener(myEventListener); 
} 
}); 

}

function initialize() { 
var application = new MyApplication(); 
} 

功能createMarker(經緯度){

var marker = new GMarker(latlng); 
    GEvent.addListener(marker,"click", function() { 
    marker.openInfoWindowHtml('Petronas Twin Tower'); 
     }); 
    return marker; 

}

提前致謝。我已經開了近2個星期了。 :(請幫我...

回答

0

不知道你是想用代碼來完成的。我假設你希望用戶只需要在地圖上標記1,在任何1次。

我有改變了代碼看起來像這樣:

map=new GMap2(document.getElementById("googleMap")); 
map.setCenter(new GLatLng(3.145423,101.696883),13); 

var myEventListener = GEvent.bind(map,"click",this,function(overlay,latlng) { 

    // remove any markers from the map each time the user clicks on the map. This makes sure 
    // the user can only have 1 marker on the map at any 1 time 
    map.clearOverlays(); 

    var marker = null; 

    if(latlng) { 
     marker = new GMarker(latlng); 
     map.addOverlay(marker); 
    } else if(overlay instanceof GMarker) { 
     //This code is now executed 
     if (marker) 
      map.removeOverlay(marker); 
    } 
}); 

爲什麼你需要刪除第二Click事件處理程序

上面的代碼不會顯示在標記上的氣球,但至少?當用戶點擊時,標記現在被移除了 在上面。

你能否提供一些關於你想要做什麼以及代碼的確切行爲的更多信息。

相關問題