鼠标点击添加标记

//点击一个标注按钮的事件,该方法的作用就是启用添加标注事件

var map_onclick;

function enable_click()

{

map_onclick = new OpenLayers.Control.Click();    

map.addControl(map_onclick);    

map_onclick.activate();

}

 

 

//注册添加添加标注Handler

//注册添加标注Event

OpenLayers.Control.Click = OpenLayers.Class(OpenLayers.Control, {                

defaultHandlerOptions: 

{

'single': true,

'double': false,

'pixelTolerance': 0,

'stopSingle': false,

'stopDouble': false

},

 

initialize: function(options) 

{

this.handlerOptions = OpenLayers.Util.extend({}, this.defaultHandlerOptions);

OpenLayers.Control.prototype.initialize.apply(this, arguments); 

this.handler = new OpenLayers.Handler.Click(this, {'click': this.trigger}, this.handlerOptions);

}, 

 

trigger: function(e) 

{                    

var lonlat = this.map.getLonLatFromPixel(e.xy);//map.getLonLatFromViewPortPx(e.xy);      

removeAll();

createMarker("<a href='http://www.handandaily.com' target='_blank'>Visit China</a>",lonlat.lon,lonlat.lat,false);                                        

}

});

 

 

 

function createMarker(html,lon,lat,isHide)

{     

var ll, popupClass, popupContentHTML;

ll = new OpenLayers.LonLat(lon,lat);

popupClass = AutoSizeFramedCloud;

popupContentHTML = html; 

addMarker(ll, popupClass, popupContentHTML, true, false);

setCenter(lon,lat);

}

 

 

 

function addMarker(ll, popupClass, html, closeBox, overflow) 

{    

var feature = new OpenLayers.Feature(markers, ll); 

feature.closeBox = closeBox;

feature.popupClass = popupClass;

feature.data.popupContentHTML = html;

feature.data.overflow = (overflow) ? "auto" : "hidden";

 

var marker = feature.createMarker();

marker.setUrl('img/marker1.png');   //ICON

marker.display(true);   

 

var markerClick = function (evt) 

{        

if (this.popup == null) 

{         

this.popup = this.createPopup(this.closeBox);

map.addPopup(this.popup);

this.popup.show();

}

else 

{

this.popup.toggle();

}

currentPopup = this.popup;

OpenLayers.Event.stop(evt);

};  

marker.events.register("mousedown", feature, markerClick);

markers.addMarker(marker);

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 matplotlib 曲线上添加自定义标注,可以使用 matplotlib 的事件处理机制来捕捉鼠标点击事件。具体的步骤如下: 1. 导入 matplotlib 和 numpy 库。 ```python import matplotlib.pyplot as plt import numpy as np ``` 2. 创建一个 matplotlib 的 figure 和 axes 对象,并绘制曲线。 ```python fig, ax = plt.subplots() x = np.linspace(0, 10, 1000) y = np.sin(x) ax.plot(x, y) ``` 3. 定义一个函数 `onclick`,它将被调用当鼠标点击事件发生时。该函数需要获取鼠标点击的坐标,然后在这个坐标上添加一个自定义的标注。 ```python def onclick(event): # 获取鼠标点击的坐标 x, y = event.xdata, event.ydata # 添加自定义标注 ax.annotate('(%s, %s)' % (x, y), xy=(x, y), xycoords='data', xytext=(-50, 30), textcoords='offset points', arrowprops=dict(arrowstyle="->")) # 更新图形 fig.canvas.draw() ``` 在这个函数中,我们使用 `ax.annotate` 方法来添加自定义标注。该方法的参数包括标注文本,标注位置,箭头属性等等。最后,我们需要调用 `fig.canvas.draw()` 来更新图形。 4. 注册事件处理函数。 ```python cid = fig.canvas.mpl_connect('button_press_event', onclick) ``` 这一行代码将 `onclick` 函数注册为鼠标点击事件的处理函数。当用户在图形上点击鼠标时,`onclick` 函数将被调用。 5. 显示图形。 ```python plt.show() ``` 完整的代码如下所示: ```python import matplotlib.pyplot as plt import numpy as np fig, ax = plt.subplots() x = np.linspace(0, 10, 1000) y = np.sin(x) ax.plot(x, y) def onclick(event): x, y = event.xdata, event.ydata ax.annotate('(%s, %s)' % (x, y), xy=(x, y), xycoords='data', xytext=(-50, 30), textcoords='offset points', arrowprops=dict(arrowstyle="->")) fig.canvas.draw() cid = fig.canvas.mpl_connect('button_press_event', onclick) plt.show() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值