SBejga/ionic2-map-leaflet

Current User Location

Closed this issue · 5 comments

how can i find current user location I try http://leafletjs.com/examples/mobile/
replace 'map' with this.map but liks

       //web location
    this.map.locate({ setView: true});

    //when we have a location draw a marker and accuracy circle
    function onLocationFound(e) {
      var radius = e.accuracy / 2;

      L.marker(e.latlng).addTo(this.map)
          .bindPopup("You are within " + radius + " meters from this point").openPopup();

          if(radius < 12){
            L.circle(e.latlng, radius).addTo(this.map);
          }

      

      this.map.setZoom(17);

    }
    this.map.on('locationfound', onLocationFound);
    //alert on location error
    function onLocationError(e) {
      alert(e.message);
    }

    this.map.on('locationerror', onLocationError);

but got error

Runtime Error
Cannot read property 'addLayer' of undefined
Stack
TypeError: Cannot read property 'addLayer' of undefined
    at NewClass.addTo (http://localhost:8100/build/main.js:126911:6)
    at NewClass.onLocationFound (http://localhost:8100/build/main.js:82577:78)
    at NewClass.fire (http://localhost:8100/build/main.js:123561:11)
    at NewClass._handleGeolocationResponse (http://localhost:8100/build/main.js:125905:8)
    at t.invoke (http://localhost:8100/build/polyfills.js:3:9655)
    at Object.onInvoke (http://localhost:8100/build/main.js:36749:37)
    at t.invoke (http://localhost:8100/build/polyfills.js:3:9606)
    at e.runGuarded (http://localhost:8100/build/polyfills.js:3:7215)
    at http://localhost:8100/build/polyfills.js:3:6843

ok .. thanks .. I downloaded latest version of before making app ..

Hey,

sorry for this late response.
I added an example for Geolocation as a branch: https://github.com/SBejga/ionic2-map-leaflet/tree/geolocation

to use do the following:

git clone -b geolocation https://github.com/SBejga/ionic2-map-leaflet 
cd ionic2-map-leaflet
npm install 
ionic serve

I hope this helps.

And I tried also to use this.map.locate({setView: true, maxZoom: 16}); which will also center the map to the location.

You can just add it to function initMap() in map.ts

  initMap() {
    this.map = L.map('map', {
      center: this.center,
      zoom: 13
    });

    //Add OSM Layer
    L.tileLayer("http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png")
      .addTo(this.map);

      this.map.locate({setView: true, maxZoom: 16});
  }

Thanks A lot..