Проблема при создании маркера в листовке - PullRequest
0 голосов
/ 10 апреля 2020

Я пытаюсь создать маркер с помощью листовки angular 8. Следующий пример работает нормально.

Но в методе addMarker, когда я изменяю значения lat и long (допустимый lat long), Маркер больше не создается. Может кто-нибудь объяснить, пожалуйста?

LAYER_OSM = tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { maxZoom: 18, attribution: 'Open Street Map' });

    // Values to bind to Leaflet Directive
    options = {
        layers: [ this.LAYER_OSM ],
        zoom: 10,
        center: latLng(46.879966, -121.726909)
    };

    markers: Layer[] = [];



    addMarker() {
        const newMarker = marker(
        [ 46.879966 + 0.1 * (Math.random() - 0.5), -121.726909 + 0.1 * (Math.random() - 0.5) ],

            {
                icon: icon({
                    iconSize: [ 25, 41 ],
          iconAnchor: [ 13, 41 ],
          iconUrl: 'leaflet/marker-icon.png',
          shadowUrl: 'leaflet/marker-shadow.png'


                })
            }
        );

        this.markers.push(newMarker);
    }

    removeMarker() {
        this.markers.pop();
    }

}

1 Ответ

1 голос
/ 10 апреля 2020

Я не уверен, где твоя проблема. Проверьте это демо. Он работает независимо от того, какие значения маркеров вы поместите

    <div
      style="height: 90vh;"
      leaflet
      [leafletOptions]="options"
      (leafletMapReady)="onMapReady($event)"
    ></div>

    <button (click)="addMarker()">
      add marker
    </button>

компонент:

  map;
  markers: L.Layer[] = [];

  popupText = "Some popup text";

  markerIcon = {
    icon: L.icon({
      iconSize: [25, 41],
      iconAnchor: [10, 41],
      popupAnchor: [2, -40],
      // specify the path here
      iconUrl: "https://unpkg.com/leaflet@1.4.0/dist/images/marker-icon.png",
      shadowUrl: "https://unpkg.com/leaflet@1.4.0/dist/images/marker-shadow.png"
    })
  };

  options = {
    layers: [
      L.tileLayer("http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
        maxZoom: 18,
        attribution: ""
      })
    ],
    zoom: 10,
    center: L.latLng(46.879966, -121.726909)
  };

  addMarker() {
    // const newMarker = L.marker(
    //   [
    //     46.879966 + 0.1 * (Math.random() - 0.5),
    //     -121.726909 + 0.1 * (Math.random() - 0.5)
    //   ],
    //   this.markerIcon
    // );
    const newMarker = L.marker([46.879966, -121.726909], this.markerIcon);

    this.markers.push(newMarker);

    newMarker.addTo(this.map);
  }

  onMapReady(map: L.Map) {
    this.map = map;
    // Do stuff with map
  }

Демо

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...