How to add markers on double-click?

We’ve learned how to add markers on map. In that example, the marker is added when the map is loaded, and its location is predetermined. How to add a marker on demand, i.e., on double-click? We also learned how to add event listeners. Now we can combine the two techniques to add markers on double-click event.

map.addEventListener("dblclick",function(e){
  var myIcon = new BMap.Icon("heart.png", new BMap.Size(48, 48), {anchor: new BMap.Size(24, 48)});  
  var marker = new BMap.Marker(e.point,{icon: myIcon});  
  map.addOverlay(marker); 
});

The handler for double-click event has an event parameter which contains the point(e.point) where the double-click happened. We just need to take that parameter to construct the marker object. Now every time a user double clicks, a marker is added to that location. If you run this example, you will find although the marker can be successfully created, the map zooms out automatically when double-clicking, which is a little distracting. This auto zoom is the default behavior of baidu map. You can disable the auto zoom on double-click by calling:

map.disableDoubleClickZoom();

 

Did you like this?
Tip admin with Cryptocurrency

Donate Bitcoin to admin

Scan to Donate Bitcoin to admin
Scan the QR code or copy the address below into your wallet to send some bitcoin:

Donate Bitcoin Cash to admin

Scan to Donate Bitcoin Cash to admin
Scan the QR code or copy the address below into your wallet to send bitcoin:

Donate Ethereum to admin

Scan to Donate Ethereum to admin
Scan the QR code or copy the address below into your wallet to send some Ether:

Donate Litecoin to admin

Scan to Donate Litecoin to admin
Scan the QR code or copy the address below into your wallet to send some Litecoin:

Donate Monero to admin

Scan to Donate Monero to admin
Scan the QR code or copy the address below into your wallet to send some Monero:

Donate ZCash to admin

Scan to Donate ZCash to admin
Scan the QR code or copy the address below into your wallet to send some ZCash:

Comments are closed, but trackbacks and pingbacks are open.