2011-02-28 108 views
1

我有這樣的代碼爲上下文菜單和它的正常工作顯示上下文菜單(標記)

google.maps.event.addListener(map, 'rightclick', function(e) 
{ 
    // start buy hiding the context menu if its open 
    contextMenu.hide(); 

    var mapDiv = $(map.getDiv()), 
     x = e.pixel.x, 
     y = e.pixel.y; 

    // save the clicked location 
    clickedLatLng = e.latLng; 

    // adjust if clicked to close to the edge of the map 
    if (x > mapDiv.width() - contextMenu.width()) 
     x -= contextMenu.width(); 

    if (y > mapDiv.height() - contextMenu.height()) 
     y -= contextMenu.height(); 

    // Set the location and fade in the context menu 
    contextMenu.css({ top: y, left: x }).fadeIn(100); 
}); 

我的問題是,爲什麼這是行不通的,同樣的事情,但點擊現在startMarker * click *?

google.maps.event.addListener(startMarker, 'click', function(e) { 
      // start buy hiding the context menu if its open 
      contextMenu.hide(); 

      var mapDiv = $(map.getDiv()), 
       x = e.pixel.x, 
       y = e.pixel.y; 

      // save the clicked location 
      clickedLatLng = e.latLng; 

      // adjust if clicked to close to the edge of the map 
      if (x > mapDiv.width() - contextMenu.width()) 
       x -= contextMenu.width(); 

      if (y > mapDiv.height() - contextMenu.height()) 
       y -= contextMenu.height(); 

      // Set the location and fade in the context menu 
      contextMenu.css({ top: y, left: x }).fadeIn(100); 
      }); 

和代碼

// Create the context menu element 
var contextMenu = $(document.createElement('ul')).attr('id', 'contextMenu'); 

// Fill our context menu with links 
contextMenu.append(
    '<li><a href=\"#startMenu\">Start</a></li>' + 
    '<li><a href=\"#stopMenu\">End</a></li>' 
); 

// Disable the browser context menu on our context menu 
contextMenu.bind('contextmenu', function() { return false; }); 

// Append it to the map object 
$(map.getDiv()).append(contextMenu); 


// Set some events on the context menu links 
contextMenu.find('a').click(function() 
{ 
    // fade out the menu 
    contextMenu.fadeOut(75); 

    // The link's href minus the # 
    var action = $(this).attr('href').substr(1); 

    switch (action) { 
     case 'startMenu': 
      $.get('something1.php', method1); 
      break; 

     case 'stopMenu': 
      $.get('something2.php', method2); 
      break; 
    } 

    return false; 
}); 

回答

3

我激活上下文菜單中右鍵單擊地圖和標記,步驟上:

1)創建一個新的MapCanvasProjection使用fromLatLngToContainerPixel功能:

overlay = new google.maps.OverlayView(); 
overlay.draw = function() {}; 
overlay.setMap(map); 

2)對於每標記包括RightClick監聽器:

google.maps.event.addListener(marker, 'rightclick', function() { 

    google.maps.event.trigger(map, 'rightclick', this); 

}); 

3)將RightClick Listener M AP具有:

g.event.addListener(this.theMap, 'rightclick', function(e) 
{ 

      try { 

      var lpx =  overlay.getProjection().fromLatLngToContainerPixel(e.getPosition()); 
      var latLng_r=e.getPosition(); 

      } catch(err) { 

      var lpx=e.pixel; 
      var latLng_r=e.latLng; 

     } 

     // Shorthand some stuff 
     var mapDiv = $(self.theMap.getDiv()), 
       menu = self.theMenu, 
         x = lpx.x, 
       y = lpx.y; 

     // Hide the context menu if its open 
     menu.hide(); 

     // Save the clicked location 
      self.clickedLatLng = latLng_r; 

     // Adjust the menu if clicked to close to the edge of the map 
     if (x > mapDiv.width() - menu.width()) 
      x -= menu.width(); 

      if (y > mapDiv.height() - menu.height()) 
      y -= menu.height(); 

     // Set the location and fade in the context menu 
       menu.css({ top: y, left: x }).fadeIn(200); 
     }); 

     // Hide context menu on several events 
     $.each('click dragstart zoom_changed maptypeid_changed center_changed'.split(' '), function(i,name){ 
      g.event.addListener(self.theMap, name, function(){ self.theMenu.hide(); }); 
     }); 
} 
0

標記事件(e)中沒有返回的像素對象或LatLng物件(不同於地圖事件)的其餘部分 - 它只返回的X & y座標事件 - 所以在你的下面幾行代碼將不會爲標記工作

x = e.pixel.x, 
y = e.pixel.y; 
clickedLatLng = e.latLng; 

爲了讓你的座標將需要更換以上

x=e.x 
y=e.y 

您將無法通過單擊標記來獲取latLng對象。我想最好的方法是從標記本身的屬性中讀取對象。

clickedLatLng = startMarker.getPosition() 
+0

但我得到真正的X,Y座標 - 我需要那些像素 - 我應該使用fromLatLngToContainerPixel與OverelayView還是...? – svenkapudija 2011-03-01 07:30:33