将传单地图定义为动态地图ID(无法读取未定义的属性“ _container”)

时间:2018-10-27 12:59:37

标签: leaflet containers

我有2个不同的div,它们通过传单用作地图对象。我想将地图对象定义为诸如

的函数

html

<div id="mymap"></div>
<div id="mymap2"></div>

javascript

var map;

function define_map(map_id) {

  var osmUrl = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
    osm = L.tileLayer(osmUrl, {
      maxZoom: 7
    }),
    map = new L.Map(map_id, {
      center: new L.LatLng(51.505, -0.04),
      zoom: 7
    }),
    drawnItems = L.featureGroup().addTo(map);
});

define_map("mymap");
define_map("mymap2");



$(".btnmarker").on("click", function() {
  console.log(map); 
  L.DomUtil.addClass(map._container, 'my_crosshair-cursor-enabled'); // ON THIS LINE GETTING ERROR CODE Cannot read property '_container' of undefined
  selected_tool = $(this).attr("id");
});

没问题,在通过按钮单击事件访问它们的功能之前,我的地图似乎运行良好(此按钮会将标记类添加到我点击过的地图上)

但是我得到的地图未定义错误。 map是一个变量,定义在页面的第一行。

感谢前进

0 个答案:

没有答案