简体   繁体   中英

Geo-location with leaflet in JS

The following JS script (CSS/HTML included for completeness) is intended to open the leaflet map tiles and mark the user location. While it opens the map, it doesn't mark the user location. Why not?

<!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>

You are not trying to get the user location at any point in this code. This line :

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

e is not defined in the code you provide. So you need to give it a latitude/longitude value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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