Home Web Front-end JS Tutorial Baidu Map adds a right-click menu to the map (determining whether it is a marker)_javascript skills

Baidu Map adds a right-click menu to the map (determining whether it is a marker)_javascript skills

May 16, 2016 pm 03:12 PM
right click menu

No more nonsense, let me just post the code for you.

js:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

var s;//经度

var w;//纬度

map.addEventListener("rightclick",function(e){

if(e.overlay){//判断右键单击的是否是marker

}else{

s = e.point.lng;//经度

w = e.point.lat;//维度

RightClick();//右键单击map出现右键菜单事件

}

});

//右键单击map出现右键菜单事件

function RightClick(){

//alert('你点击的是地图');

var createMarker = function(map){//右键更新站名

if (confirm("要新建站点吗?")){

if(true){

$(".AllSetMassage").show();

}

}

};

var markerMenu=new BMap.ContextMenu();

markerMenu.addItem(new BMap.MenuItem('新建站点',createMarker.bind(map)));

map.addContextMenu(markerMenu);//给标记添加右键菜单

}

Copy after login

Effect:

Right click on map (non-marker)


Right click on marker:

PS: Baidu Maps adds right-click menus (delete, update) for multiple markers

js:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

$.getJSON("./GetStationPlaceServlet",function(json){

for(var i=;i<json.length;i++){

var obj = eval(json);

//获取经纬度

fStationlon = parseFloat(obj[i].Stationlon);

fStationlat = parseFloat(obj[i].Stationlat);

var pt = new BMap.Point(fStationlon,fStationlat);

var myIcon = new BMap.Icon("photo/station.png",new BMap.Size(,),{ anchor: new BMap.Size(, ) });

var marker = new BMap.Marker(pt,{icon:myIcon}); // 创建标注

map.addOverlay(marker); //将标注添加到地图中

var strRes = parseFloat(obj[i].StationId);

addClickHandler(strRes,marker);//左键单击marker事件

RightClickHandler(strRes,marker);//右键单击marker出现右键菜单事件

RightClick();//右键地图出现右键菜单事件

}

});

//右键单击marker出现右键菜单事件

function RightClickHandler(stationId,marker){

var removeMarker = function(e,ee,marker){//右键删除站点

var json={

"StationId":stationId,

};

if (confirm("要删除站点"+stationId+"吗?")){

if(true){

$.getJSON("./DeleteStationServlet",{json:JSON.stringify(json)},function(json){

if(json.result==true){

alert("删除站点"+stationId+"成功!");

map.removeOverlay(marker); //将地图中的标记删除

}

});

}

}

};

var updateMarker = function(marker){//右键更新站名

if (confirm("要修改站点"+stationId+"的站名吗?")){

if(true){

$(".AllUpdateMassage").show();

$("#stationId").val(stationId);

}

}

};

var markerMenu=new BMap.ContextMenu();

markerMenu.addItem(new BMap.MenuItem('删除站点',removeMarker.bind(marker)));

markerMenu.addItem(new BMap.MenuItem('修改站名',updateMarker.bind(marker)));

marker.addContextMenu(markerMenu);//给标记添加右键菜单

}

//鼠标左键单击marker事件

function addClickHandler(stationId,marker){

marker.addEventListener("click",function(e){

var p = marker.getPosition(); //获取marker的位置

var staId={

"StationId":stationId,

};

$.getJSON("./GetStationInfoServlet",{json:JSON.stringify(staId)},function(json){

for(var i=;i<json.length;i++){

var obj = eval(json);

stationName = obj[i].stationName;

var content = "站点编号:"+stationId+"<br/>"+ "站点名称:"+stationName;

openInfo(content,e);

}

});

});

}

//左键单击marker弹出窗口事件

function openInfo(content,e){

var p = e.target;

var point = new BMap.Point(p.getPosition().lng, p.getPosition().lat);

var infoWindow = new BMap.InfoWindow(content,opts); // 创建信息窗口对象

map.openInfoWindow(infoWindow,point); //开启信息窗口

}

//修改站点站名

function update(){

var stationId=$("#stationId").val();//编号

var stationName=$(".AllUpdateMassage_name").val();//名字

var json={

"StationId":stationId,

"StationName":stationName

};

$.getJSON("./UpdateStationServlet",{json:JSON.stringify(json)},function(json){

if(json.result==true){

alert("站点"+stationId+"站名修改成功!");

$(".AllUpdateMassage").hide();

}

else{

alert("失败!");

}

});

}

Copy after login

Rendering:


This information has been deleted from the map and database:

When I was implementing this, I found that someone could only add a right-click menu to a marker. I don't have this problem. The principle seems to be about closures or something. I am a newbie and I don’t know it very well. I hope someone who understands will give me some advice.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the slow response of Win11 right-click menu? Solution to the slow pop-up menu of right-clicking the mouse in Win11 How to solve the slow response of Win11 right-click menu? Solution to the slow pop-up menu of right-clicking the mouse in Win11 Jan 31, 2024 pm 10:06 PM

After upgrading the Win11 system, some friends found that the pop-up menu when right-clicking the mouse has become slower. What is going on? Is it because of the configuration problem, or what kind of settings should be made? The editor of this website will tell you how to solve it today. In fact, we can adjust the performance to the highest performance, so that there will be no problem of slow response. Solution to the slow right-click pop-up menu of Win11 1. First, find the system settings in the start menu. 3. Then find the advanced system settings in the relevant links below. 5. Then under the visual effects tab, check Adjust for best performance.

What should I do if there is no compressed package option when right-clicking on Windows 10? What should I do if there is no compressed package option when right-clicking on Windows 10? Jul 13, 2023 pm 11:05 PM

Win10 system has built-in decompression software WinRAR. Generally, you can find the shrink option by right-clicking on the folder. However, some users find that there is no compressed package option when they right-click. What should they do? You first find WinRAR in the menu bar to open, then click the option above, then select Settings, switch to the integrated menu bar in the opening window, click the context menu button, and check "Add to archive". What to do if there is no compressed package option in win10: You can click the "Start/WinRar/Winrar" menu items on the Windows 10 desktop. At this time, the main interface of Winrar software will open, click on the "Options" menu item above. In the drop-down list of options that pops up, click "Settings"

How to add a drawing program to the right-click menu of Win11? How to add a drawing program to the right-click menu of Win11 How to add a drawing program to the right-click menu of Win11? How to add a drawing program to the right-click menu of Win11 Feb 11, 2024 pm 09:03 PM

Many win11 users want to add their drawing program to the right-click menu, so how do they add it? Users can directly right-click the Windows Terminal under Start, and then directly copy the following code to the corresponding location. Let this site carefully introduce to users how to add a drawing program to the Win11 right-click menu. How to add a drawing program to the Win11 right-click menu 1. First, right-click the "Start" button (or press the Win+X keys) and select "Windows Terminal (Administrator)". 2. Then paste this string of code into the terminal application [reg.exeadd&ldqu

Right click and create new word disappears Right click and create new word disappears Dec 25, 2023 am 10:43 AM

Many friends find that Word, ppt, and Excel in the list are missing when they right-click to create a new one. What is going on? In fact, some files are missing from the registration list. We only need to enter the settings to adjust the values. Okay, let’s take a look at the specific tutorial with the editor below. Method to fix the problem of missing right-click New Word 1. Click Start, find the run command line, enter regedit, and open the registry. 2. Find the HKEY_CLASSES_ROOT directory on the left and expand it. 3. First, we use the CTRL+F shortcut keys to find the location of "docx". Of course, you can also pull down and search by yourself. 4. Once found, there is no need to expand the directory. We can see that there is a (

How to solve the problem of stuck right-click menu of win11 file How to solve the problem of stuck right-click menu of win11 file Jul 02, 2023 pm 11:13 PM

How to solve the problem of stuck right-click menu of win11 files? What should I do if I need to view the specific attributes of a file? We often left-click to select a file and then right-click to open the menu to view properties. However, many friends who use the win11 version of the operating system often encounter that when right-clicking on a file, they always have to wait for a while before the right-click menu is displayed. This has a great impact on users' computer experience. In this regard, the editor uses the following article to introduce how to solve this problem. Many friends don’t know how to operate in detail. The editor below has compiled a tutorial on how to solve the problem of Win11 right-click menu being stuck. If you are interested, follow the editor and take a look below! Overview of the tutorial on how to fix the stuck win11 right-click menu 1. First, we press [ctr] at the same time

What to do if the right-click menu management cannot be opened in Windows 10 What to do if the right-click menu management cannot be opened in Windows 10 Jan 04, 2024 pm 07:07 PM

When we use the win10 system, when we use the mouse to right-click the desktop or the right-click menu, we find that the menu cannot be opened and we cannot use the computer normally. At this time, we need to restore the system to solve the problem. Win10 right-click menu management cannot be opened: 1. First open our control panel, and then click. 2. Then click under Security and Maintenance. 3. Click on the right to restore the system. 4. If it still cannot be used, check whether there is something wrong with the mouse itself. 5. If you are sure there is no problem with the mouse, press + and enter. 6. After the execution is completed, restart the computer.

How to set the right-click menu not to collapse in win11? How to set the right-click menu not to collapse in win11? Jan 29, 2024 pm 02:30 PM

Many users find that the right-click menu is not very easy to use after upgrading to win11. This makes many users ask how to set the right-click menu of win11 without folding? In fact, the method is very simple. Users can enter the taskbar settings and perform operations. Let this site carefully introduce the win11 right-click menu non-folding setting tutorial for users. Win11 right-click menu non-folding setting tutorial 1. Right-click the empty space of the taskbar to enter the taskbar settings. 3. Click the open icon and it will be displayed directly. If it is closed, it will be stored in the small triangle.

Win11 mouse right-click menu setting tutorial Win11 mouse right-click menu setting tutorial Dec 23, 2023 am 11:05 AM

Since win11 has changed the mouse right-click menu, many friends feel uncomfortable using it. They want to know how to set up the win11 mouse right-click menu. In fact, we only need to open the registry, where we can edit the right-click menu. How to set the mouse right-click menu in win11: 1. First press "Win+R" on the keyboard to open run. 2. Enter "regedit" and press Enter to open the registry. 3. After opening, go to the "Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FeatureManagement\Overrides\4" location. 4. Then right-click the 4 folder and select New

See all articles