2016-07-17 9 views
0

Das folgende JS-Skript (CSS/HTML ist der Vollständigkeit halber enthalten) soll die Faltblattkacheln öffnen und den Benutzerstandort markieren. Während die Karte geöffnet wird, wird der Benutzerstandort nicht markiert. Warum nicht?Geo-location with leaflet in JS

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

Antwort

2

Sie versuchen nicht, den Benutzerstandort an irgendeiner Stelle in diesem Code zu erhalten. Diese Zeile:

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

e ist nicht im Code definiert Sie. Sie müssen also einen Breiten-/Längengrad angeben.

+0

Vielen Dank. Das Folgende korrigiert und behebt es. \t \t \t Funktion onLocationFound (e) { \t \t \t \t var radius = e.accuracy/2; \t \t \t \t L.marker (e.latlng) .addTo (Karte) \t \t \t \t \t .bindPopup ("Sie sind in" + Radius + "Meter von diesem Punkt") openPopup(). \t \t \t \t L.Kreis (e.Latlng, Radius) .addTo (Karte); \t \t \t} \t \t \t \t \t \t Funktion onLocationError (e) { \t \t \t \t alert (e.message); \t \t \t} \t \t \t map.on ('locationfound', onLocationFound); \t \t \t map.on ('locationerror', onLocationError); \t \t \t map.locate ({setView: true, maxZoom: 16}); – Iorek