google-maps - 单击时显示上下文菜单(标记)

标签 google-maps contextmenu

我有上下文菜单的代码,它工作正常

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 *点击*?

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;
});

最佳答案

我必须激活上下文菜单,右键单击 map 并标记,步骤:

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 监听器映射替换为:

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(); });
        });
}

关于google-maps - 单击时显示上下文菜单(标记),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5147923/

相关文章:

Android:在自定义 WebView 中从 onLongPress 打开 ContextMenu

android - 使用系统毫秒循环一个 Action android java

javascript - 迭代数组并向每个数组添加谷歌地图事件?

Android:如何防止拖动被解释为长按

c# - 将项目添加到上下文菜单和这些项目的自定义操作

jquery - CSS 和 jQuery contextMenu 插件

javascript - 谷歌地图 : get radius of circle overlay while creating circle

ios - 如何使用适用于 iOS 的 Google Maps SDK 绘制圆圈?

javascript - 将谷歌地图点的坐标提取到 HTML 中的谷歌表格

java - 我应该使用什么命令来使用 Windows 注册表从上下文菜单启动 jar 文件?