Javascript 谷歌地图V3上下文菜单

Javascript 谷歌地图V3上下文菜单,javascript,google-maps-api-3,contextmenu,Javascript,Google Maps Api 3,Contextmenu,我正在寻找谷歌地图V3上下文菜单库。我在这里找到了一些代码示例 4月份的堆栈溢出问题也刚刚提出了上述示例。我也是 但也许有人已经将这些示例封装在一个可重用的库中,或者同时发现了一些东西。很明显,V2是有问题的 --更新日期:2012-05-31-- 我找到了另一个,但还没有时间测试它。我想你不需要一个库。首先,我会尝试: var contextMenu = google.maps.event.addListener( map, "rightclick",

我正在寻找谷歌地图V3上下文菜单库。我在这里找到了一些代码示例

  • 4月份的堆栈溢出问题也刚刚提出了上述示例。我也是

    但也许有人已经将这些示例封装在一个可重用的库中,或者同时发现了一些东西。很明显,V2是有问题的

    --更新日期:2012-05-31--


    我找到了另一个,但还没有时间测试它。

    我想你不需要一个库。首先,我会尝试:

    var contextMenu = google.maps.event.addListener(
            map,
            "rightclick",
            function( event ) {
                // use JS Dom methods to create the menu
                // use event.pixel.x and event.pixel.y 
                // to position menu at mouse position
                console.log( event );
            }
        );
    
    这假设您的地图是通过以下方式创建的:

    var map = new google.maps.map( { [map options] } );
    
    回调中的
    事件
    对象有4个属性

  • latLng
  • ma
  • pixel

  • 其中,
    pixel.x
    pixel.y
    是点击事件触发的偏移量-从保存地图对象的画布的左上角开始计算。

    我创建了一个工作JS提琴,用于显示上下文菜单,以及在此上下文菜单上具有可点击项的功能

    当在Google地图上右键单击标记时,它会显示一个可单击的上下文菜单。 基本上,它利用了地图上的覆盖视图。顺便说一句,这只是一个演示

    var loc, map, marker, contextMenu;
    
    ContextMenu.prototype = new google.maps.OverlayView();
    
    /**
      * onAdd is called when the map's panes are ready and the overlay has been
      * added to the map.
      */
    ContextMenu.prototype.onAdd = function() {
    
        $("<div id='cMenu' class='context-menu-marker'></div>").appendTo(document.body);
        var divOuter = $("#cMenu").get(0);
    
        for(var i=0;i < this.menuItems.length;i++) {
            var mItem = this.menuItems[i];
            $('<div id="' + mItem.id + '" class="options-marker">' +
              mItem.label + '</div>').appendTo(divOuter);
        }
    
        this.div_ = divOuter;
    
        // Add the element to the "overlayLayer" pane.
        var panes = this.getPanes();
        //panes.overlayLayer.appendChild();
        panes.overlayMouseTarget.appendChild(this.div_);
    
        var me = this;
    
        for(var i=0;i < this.menuItems.length;i++) {
            var mItem = this.menuItems[i];
    
            var func = function() {
               me.clickedItem = this.id;
               google.maps.event.trigger(me, 'click');
            };
    
            google.maps.event.addDomListener($("#" + mItem.id).get(0), 'click', $.proxy(func, mItem));
        }
    
    
        google.maps.event.addListener(me, 'click', function() {
           alert(me.clickedItem); 
        });
    
    };
    
    ContextMenu.prototype.draw = function() {
        var div = this.div_;
        div.style.left = '0px';
        div.style.top = '0px';
        div.style.width = '100px';
        div.style.height = '50px';
    };
    
    // The onRemove() method will be called automatically from the API if
    // we ever set the overlay's map property to 'null'.
    ContextMenu.prototype.onRemove = function() {
        this.div_.parentNode.removeChild(this.div_);
        this.div_ = null;
    };
    
    // Set the visibility to 'hidden' or 'visible'.
    ContextMenu.prototype.hide = function() {
      if (this.div_) {
        // The visibility property must be a string enclosed in quotes.
        this.div_.style.visibility = 'hidden';
      }
    };
    
    ContextMenu.prototype.show = function(cpx) {
      if (this.div_) {
        var div = this.div_;
        div.style.left = cpx.x + 'px';
        div.style.top = cpx.y + 'px';
    
        this.div_.style.visibility = 'visible';
      }
    };
    
    function ContextMenu(map,options) {
        options = options || {}; //in case no options are passed to the constructor
        this.setMap(map); //tells the overlay which map it needs to draw on
        this.mapDiv = map.getDiv(); //Div container that the map exists in
        this.menuItems = options.menuItems || {}; //specific to context menus
        this.isVisible = false; //used to hide or show the context menu
    }
    
    function initialize() {
    
        loc = new google.maps.LatLng(62.323907, -150.109291);
    
        var options = {};
        var menuItems=[];
    
        menuItems.push({id:"zoomIn", className:'context_menu_item', eventName:'zoom_in_click', label:'Zoom in'});
        menuItems.push({id:"zoomOut", className:'context_menu_item', eventName:'zoom_out_click', label:'Zoom out'});
    
    
        options.menuItems = menuItems;
        //=========================================
    
        map = new google.maps.Map(document.getElementById("map"), {
            zoom: 12,
            center: loc,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        });
    
        marker = new google.maps.Marker({
            map: map,
            position: loc,
            visible: true
        });
    
        contextMenu = new ContextMenu(map, options);
    
        google.maps.event.addListener(marker, 'rightclick', function(mouseEvent){
            contextMenu.hide();
            this.clickedMarker_ = this;
            var overlayProjection = contextMenu.getProjection();
            var cpx = overlayProjection.fromLatLngToContainerPixel(mouseEvent.latLng);
            contextMenu.show(cpx);
    
            map.setOptions({ draggableCursor: 'pointer' });
        });
    
        // Hide context menu on several events
        google.maps.event.addListener(map,'click', function(){
            map.setOptions({ draggableCursor: 'grab' });
            contextMenu.hide();
        });
    
    }
    
    google.maps.event.addDomListener(window, 'load', initialize);
    
    var-loc、地图、标记、上下文菜单;
    ContextMenu.prototype=new google.maps.OverlayView();
    /**
    *当地图的窗格准备就绪且覆盖已完成时,将调用onAdd
    *添加到地图中。
    */
    ContextMenu.prototype.onAdd=函数(){
    $(“”).appendTo(document.body);
    var divOuter=$(“#cMenu”).get(0);
    for(var i=0;i
    小提琴链接:


    访问此演示网站:

    对于上下文菜单,我不建议实现GoogleMapsAPI提供的OverlyView类的一个子类。首先,应该在Google提供的五个窗格中添加
    overlayView
    子类的一个实例。更可能的情况是,应该将此实例添加到窗格
    OverlyMouseTarget
    但是,此实例被其上的其他dom“隐藏”。因此,正常的原始浏览器事件,如
    mouseover
    mouseout
    无法到达此实例

    必须使用Google Maps API方法:
    addDomListener
    来处理它()。它需要大量JavaScript代码来实现不同的事件处理程序,需要添加和删除大量css类来实现一些视觉效果,如果此实例在映射容器之外,则可以使用几行css代码来实现

    所以事实上
    <div id="mapcover">
      <div id="mapcover-map"></div> <!-- this is map container-->
      <div id="demoControlPanel" class="mc-static2mapcontainer panel">I am map UI control button's container, I think one can use jQuery UI to make me look better<br><br>
        <div id="zoom-in-control" class="text-center">zoomIn</div>
        <div id="zoom-out-control" class="text-center">zoomOut</div>
      </div>
      <div id="demoContextPanel" class="mc-ascontextmenu panel">
    
        I am map context menu container, you can sytle me and add logic to me, just as normal DOM nodes.
        since I am not in Map Container at all! 
        <div class="text-center">
          <div role="group" aria-label="..." class="btn-group">
            <button id="place-marker1" type="button" class="btn btn-default">Marker1</button>
            <button id="place-marker2" type="button" class="btn btn-default">Marker2</button>
          </div>
        </div>
        <div class="form-group">
          <label for="content-marker1">Content of next Marker1</label>
          <input id="content-marker1" type="text" placeholder="New of Marker1!" class="form-control">
        </div>
      </div>
    </div>