【问题标题】:map marker in script tags within ejs file not showingejs文件中脚本标签中的映射标记未显示
【发布时间】:2016-12-01 15:25:52
【问题描述】:

我想要两个标记,一个是用户的当前位置,另一个是我的应用中帖子的位置。
用户当前位置的标记被绘制在地图上,而不是属于帖子位置的标记。
帖子包含具有经纬度对的感兴趣地点的信息。

我用这样的帖子渲染了一个 ejs 视图:

router.get('/posts/:id', function(req,res) {
  db.post.findOne({
    where: { id: req.params.id}
  }).then(function(post) {
    res.render('posts/post_detail', {post: post})
  });
});

post 包含我想要访问的纬度和经度信息,以便在我的传单地图上绘制标记。我将脚本标签放在 ejs 视图的底部,如下所示:

<script>
  var post = <%- JSON.stringify(post) %>;
  console.log(post.latitude);
  console.log(post.longitude);
  L.marker([post.latitude, post.longitude]).addTo(map);
  console.log(map)
</script>

在控制台上打印出的纬度和经度与数据库中的值相同。我在其他地方使用相同的 L.marker 代码在我的地图上放置标记,它工作得非常好,但在这种情况下不是。我得到了--

Uncaught TypeError: t.addLayer is not a function error

还有其他方法可以访问纬度和经度值以在地图上绘制标记吗?

【问题讨论】:

  • 什么是 L.marker?这是您的代码中可用的功能吗?你能显示那个代码吗?
  • L.marker 是一个传单。我已经像这样使用它来绘制用户的当前位置并且它工作正常。 L.marker([latitude, longitude]).addTo(map).bindPopup("你在这里").openPopup();

标签: node.js express leaflet ejs


【解决方案1】:

地图未定义在该脚本标记的范围内。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多