简体   繁体   English

点击鼠标左键在Google地图上添加标记。 试图修改一个例子

[英]Add marker on Google map on button left click. Trying to modify one example

Here is example https://hpneo.github.io/gmaps/examples/context_menu.html 这是示例https://hpneo.github.io/gmaps/examples/context_menu.html

Copy the code here 将代码复制到此处

map.setContextMenu({
  control: "map",
  options: [{
    title: "Add marker",
    name: "add_marker",
      action: function(e) {
      this.addMarker({
      lat: e.latLng.lat(),
      lng: e.latLng.lng()
      });
    }
  }]
});

On right click see Add marker . 右键单击,请参阅Add marker Left click on marker and see marker on the map. 左键单击标记,然后在地图上查看标记。

But want to add marker on left click 但是要在左键上添加标记

Tried 试着

map = new GMaps({
  click: function(event) {

  //alert("click");
  var lat=event.latLng.lat();
  var lng=event.latLng.lng();
  alert ( lat+ ", "+ lng );

   this.addMarker({
   lat: event.latLng.lat(),
   lng: event.latLng.lng(),
   });

  }
});

When left click works alert ( lat+ ", "+ lng ); 当单击鼠标左键时会发出alert ( lat+ ", "+ lng ); , but no marker added. ,但未添加任何标记。 What need to correct? 需要纠正什么?

Solution appears very simple 解决方案看起来很简单

map = new GMaps({
  click: function(event) {

  //alert("click");
  var lat=event.latLng.lat();
  var lng=event.latLng.lng();
  alert ( lat+ ", "+ lng );

   map.addMarker({
   lat: lat,
   lng: lng,
   });

  }
});

//This is just a snippet. //这只是一个片段。 Please read InfoWindow and Marker from Google Map API Library 请阅读Google Map API库中的InfoWindow和Marker

var infowindow = new google.maps.InfoWindow({
    content: '' //content is your HTML/div element  with a menu this is where your menu context is located
});


 var marker = new google.maps.Marker({
    title: 'your title',
    position: new google.maps.LatLng(yourlat, yourlng),
    map: map //your map
});


google.maps.event.addListener(marker, 'click', function() { 
    infowindow.setContent(html); 
    infowindow.open(map, marker); 
}); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM