单击时显示上下文菜单(标记)
我有上下文菜单的代码,它工作正常
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;
});
I have this code for context menu and it's working fine
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);
});
My question is why this isn't working, the same thing but click is now 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);
});
And the rest of the code
// 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;
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我必须激活上下文菜单,右键单击地图并进行标记,步骤:
1)创建一个新的 MapCanvasProjection 以使用 fromLatLngToContainerPixel 函数:
2)对于每个标记,包括 RightClick 侦听器:
3)将 RightClick 侦听器地图替换为:
I got to activate the context menu right click on the map and mark, steps:
1) Create a new MapCanvasProjection to use fromLatLngToContainerPixel function:
2) For Each Marker include RightClick Listener:
3) Replace the RightClick Listener Map with:
标记事件 (e) 不返回像素对象或 latLng 对象(与地图事件不同) - 它仅返回 x 和 latLng 对象。事件的 y 坐标 - 因此代码中的以下行不适用于标记
要获取坐标,您需要将上面的行替换为 您
将无法通过单击标记来获取 latLng 对象。我认为最好的方法是从标记本身的属性中读取对象。
Marker event (e) does not return a pixel object or a latLng object (unlike a map event) - it only returns x & y coordinates of the event - so the following lines in your code will not work for the marker
To get coordinates you will need to replace the lines above with
You will not be able to get latLng object from clicking on the marker. I suppose best way to get that is to read the object from the property of the marker itself.