在Leaflet.js中添加标记

3

如果这很简单,我道歉,但我对此不太熟悉,并已阅读并调整了多个示例,才达到当前的进展。

我正在为一次活动制作地图。我使用了leaflet来完成这项工作,并经过大量调试,最终使用了单个图像(这正是我想要的,因为它将经常更新)。

下一步是在感兴趣的位置添加标记。现在我不特别想要任何花哨的东西,我想使用标记和弹出窗口(如此处所示:http://leafletjs.com/examples/quick-start/example-popups.html ),但是我尝试过的各种方法,从各种示例和教程中查看后似乎都无法工作。

我会粘贴我的代码,非常感谢任何能够提供帮助的人。再次道歉,如果这真的很简单,所有我有限的知识都来自于阅读和调整示例。

大部分内容都是使用此教程构建的:http://kempe.net/blog/2014/06/14/leaflet-pan-zoom-image.html

<!DOCTYPE html> 

<html> 

<head> 

<meta http-equiv="content-type" content="text/html; charset=utf-8"/> 

<link rel="icon" type="image/png" href="assets/icon.ico">

<title>Whispers of Valentia</title>         



<link rel="stylesheet" href="map.css" type="text/css" />    

<link href='http://fonts.googleapis.com/css?family=Cantora+One|Acme|Meddon|Jim+Nightshade' rel='stylesheet' type='text/css'>

    <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css">

</head> 

<body>

    <div id="navigationBar">


        <div id="navigationLogo">

                    <abbr title="This is a test map.">WHISPERS OF VALENTIA</abbr>

        </div>


    </div>

    <div class="shadeBar"></div>


    <div id="image-map" style="background: #1e2831;">


    <script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>

<script>

// Using leaflet.js to pan and zoom a big image.

// See also: http://kempe.net/blog/2014/06/14/leaflet-pan-zoom-image.html

// create the slippy map

var map = L.map('image-map', {

  minZoom: 2,

  maxZoom: 6,

  center: [5000, 2400],

  zoom: 2,

  crs: L.CRS.Simple

});

// dimensions of the image

var w = 10000,

    h = 4800,

    url = 'valentiatest.jpg';

// calculate the edges of the image, in coordinate space

var southWest = map.unproject([0, h], map.getMaxZoom()-1);

var northEast = map.unproject([w, 0], map.getMaxZoom()-1);

var bounds = new L.LatLngBounds(southWest, northEast);


// add the image overlay, 

// so that it covers the entire map

L.imageOverlay(url, bounds).addTo(map);

// tell leaflet that the map is exactly as big as the image

map.setMaxBounds(bounds);

var marker = L.marker([1000, 1000]).addTo(map);


</script>

    </div>

</body>

1个回答

2
您的代码看起来是正确的。 您使用了以下代码来添加标记:
var marker = L.marker([1000, 1000]).addTo(map);

请注意,这会在以下位置添加一个标记: 纬度:1000; 经度:1000;

这是不正确的,并且不会显示在你的地图上。 请根据你的中心添加正确的位置来添加它。

查看 Leaflet 示例代码和文档以获取更多信息: http://leafletjs.com/examples/quick-start/


在我制作地图的示例中,它说使用像素作为坐标。我已经使用这些像素坐标设置了中心点,但是标记是否基于我放置中心的经纬度运行? - Lloydski
从文档中得知:http://leafletjs.com/reference-1.0.3.html#marker 标记是GPS位置坐标;不是像素。 - aorfevre
非常感谢您的帮助!我会继续处理这个问题。 - Lloydski
我有,但它说它不会公开显示。再次感谢。 - Lloydski

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接