在JS中使用传单的地理位置

时间:2016-07-17 17:32:36

标签: javascript geolocation leaflet

以下JS脚本(完整性包含CSS / HTML)旨在打开传单地图图块并标记用户位置。在打开地图时,它不会标记用户位置。为什么不呢?

<!DOCTYPE html>

<html>

    <head>

        <title>Leaflet GeoLocation</title>

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

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

        <style>
            body {
                padding: 0;
                margin: 0;
            }
            html, body, #map {
                height: 100%;
            }
        </style>

    </head>

    <body>

        <div id="map"></div>

        <script>

            var map = L.map('map').setView([43, -79], 18);

            L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
                attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>',
                maxZoom: 18
            }).addTo(map);

            var newMarker = new L.marker(e.latlng).addTo(map);

        </script>

    </body>

</html>

1 个答案:

答案 0 :(得分:2)

您并未尝试在此代码中的任何位置获取用户位置。 这一行:

var newMarker = new L.marker(e.latlng).addTo(map);
您提供的代码中未定义

e。所以你需要给它一个纬度/经度值。

相关问题