2015-10-05 116 views
-2
 var directionDisplay; 
    var map; 
    var markersArray = []; 
    var marker; 
    var cpx; 
    var contextMenu; 
    ContextMenu.prototype = new google.maps.OverlayView(); 

    function initialize() { 

    var options = {}; 
    var menuItems=[]; 

    menuItems.push({id:"departure_point", className:'context_menu_item', eventName:'departure_point_click', label:'Set as departure point'}); 
    menuItems.push({id:"destination_point", className:'context_menu_item', eventName:'destination_point_click', label:'Set as destination point'}); 

    options.menuItems = menuItems; 

     directionsDisplay = new google.maps.DirectionsRenderer(); 
     var melbourne = new google.maps.LatLng(-26.2041028, 28.0473051); 
     var myOptions = { 
     zoom:12, 
     mapTypeId: google.maps.MapTypeId.ROADMAP, 
     center: melbourne 
     } 

     map = new google.maps.Map(document.getElementById("map"), myOptions); 
     contextMenu = new ContextMenu(map, options);   

     directionsDisplay.setMap(map); 


     google.maps.event.addListener(map, "click", function(event) 
     { 
      // place a marker 
      placeMarker(event.latLng); 

      google.maps.event.addListener(marker, 'click', function(mouseEvent){ 
       contextMenu.hide(); 
       this.clickedMarker_ = this; 
       var overlayProjection; 

       // Wait for idle map 
       google.maps.event.addListener(map, 'idle', function() { 
        // Get projection 
        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(); 
      }); 
      // marker.addListener('click', function() { 
      //  infowindow.open(map, marker); 
      // }); 
     }); 
    } 
    /** 
     * 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 placeMarker(location) { 
     // first remove all markers if there are any 
     deleteOverlays(); 

     marker = new google.maps.Marker({ 
      position: location, 
      map: map, 
     }); 

     // add marker in markers array 
     markersArray.push(marker); 


    } 

所有我想创建一个文本菜单,每当我上的标记单击它应该给用户两种选择类型错误:overlayProjection未定义

  1. 集作为出发
  2. 设置为目的地。

当我点击地图上的标记创建,但当我点击标记我得到上述错误,请帮我解决这个问题。谢谢

+1

尝试把CPX变量中上面的括号。 – sitilge

+0

嗨@Alex谢谢你回到我身边,我这样做,我认为我有点修复了错误,但我没有得到所需的输出 –

回答

0

当你在回调之外使用它时,你的overlayProjection还没有准备好。这:

google.maps.event.addListener(marker, 'click', function(mouseEvent){ 
      contextMenu.hide(); 
      this.clickedMarker_ = this; 
      var overlayProjection; 

      // This will happen at a certain later time 
      google.maps.event.addListener(map, 'idle', function() { 
       // Get projection 
       overlayProjection = contextMenu.getProjection(); 
      }); 
      var cpx = overlayProjection.fromLatLngToContainerPixel(mouseEvent.latLng); 
      // overlayProjection not yet defined 
      contextMenu.show(cpx); 

      map.setOptions({ draggableCursor: 'pointer' }); 
     }); 

也可以被看作是(A简化样本!):

google.maps.event.addListener(marker, 'click', function(mouseEvent){ 
      contextMenu.hide(); 
      this.clickedMarker_ = this; 
      var overlayProjection; 

      // .... 
      var cpx = overlayProjection.fromLatLngToContainerPixel(mouseEvent.latLng); 
      // overlayProjection not yet defined 
      contextMenu.show(cpx); 

      map.setOptions({ draggableCursor: 'pointer' }); 
     }); 

解决方法:你可以将它放入回调:

google.maps.event.addListener(marker, 'click', function(mouseEvent){ 
      contextMenu.hide(); 
      this.clickedMarker_ = this; 
      var overlayProjection; 

      // This will happen at a certain later time 
      google.maps.event.addListener(map, 'idle', function() { 
       // Get projection 
       overlayProjection = contextMenu.getProjection(); 
       var cpx = overlayProjection.fromLatLngToContainerPixel(mouseEvent.latLng); 
       contextMenu.show(cpx); 

      }); 

      map.setOptions({ draggableCursor: 'pointer' }); 
     }); 
+0

谢谢你,这实际上解决了我的问题,很多赞赏 –

+0

感谢您的upvote我的答案 .. –