Bounds is not valid

℡╲_俬逩灬. 提交于 2019-12-24 17:06:11

问题


I'm trying to implement a leaflet map where the zoom level dynamically encapsulates my 3 markers. I have looked for related questions about this and have discovered this is the most popular way:

const group = L.featureGroup(this.markerLayers);
map.fitBounds(group.getBounds()); 

However I get the error "bounds is not valid". I'm new to leaflet altogether, any idea what the problem is?

HTML:

<div class="leaflet-container grow z-0" leaflet [leafletOptions]="options" [leafletZoom]="leafletZoom" [leafletCenter]="leafletCenter" (leafletMapReady)="onMapReady($event)">
    <div [leafletLayer]="tileLayer"></div>
    <div *ngFor="let marker of markerLayers " [leafletLayer]="marker"></div>
  </div>

Component (my attempt is in the 'onMapReady' function):

import { Component, Input, OnChanges } from '@angular/core';
import * as L from 'leaflet';
import { Roadtrip, Landmark } from '../../detour.model';
import { Subject } from 'rxjs/Subject';
import { DetourService } from '../../detour.service';

@Component({
  selector: 'app-map',
  templateUrl: './map.component.html',
  styleUrls: ['./map.component.scss']
})
export class MapComponent implements OnChanges {
  @Input() selectedRoadTrip: Roadtrip;
  public fitBounds: L.LatLngBounds;
  public markers;
  public markerLayers: L.Marker[] = [];
  public leafletZoom = 8;
  public leafletCenter = L.latLng(57.335, -95.173);
  public tileLayer = L.tileLayer(
    'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'
  );
  public iconImagePath: '/assets/icons/';
  public map$ = new Subject<L.Map>();
  public options = {
    zoomControl: false,
  };

  constructor(public detourService: DetourService) {}

  ngOnChanges() {
    if (this.selectedRoadTrip) {
      this.mapInit(this.selectedRoadTrip);
    }
  }

  mapInit(roadtrip: Roadtrip) {
    const landmark1 = roadtrip.landmarks[0];
    const landmark2 = roadtrip.landmarks[1];
    const landmark3 = roadtrip.landmarks[2];

    const marker1 = L.marker([landmark1.latitude, landmark1.longitude], {
      icon: L.icon({
        iconUrl: this.detourService.getLandmarkPinImage(landmark1.category),
        iconSize: [30, 48],
        iconAnchor: [15, 48]
      })
    });
    const marker2 = L.marker([landmark2.latitude, landmark2.longitude], {
      icon: L.icon({
        iconUrl: this.detourService.getLandmarkPinImage(landmark2.category),
        iconSize: [30, 48],
        iconAnchor: [15, 48]
      })
    });
    const marker3 = L.marker([landmark3.latitude, landmark3.longitude], {
      icon: L.icon({
        iconUrl: this.detourService.getLandmarkPinImage(landmark3.category),
        iconSize: [30, 48],
        iconAnchor: [15, 48]
      })
    });

    this.leafletCenter = L.latLng([roadtrip.latitude, roadtrip.longitude]);
    this.markerLayers = [marker1, marker2, marker3];
  }

  onMapReady(map: L.Map) {
    map.addControl(L.control.zoom({position: 'bottomright'}));
    map.scrollWheelZoom.disable();
    const group = L.featureGroup(this.markerLayers);
    map.fitBounds(group.getBounds());
    this.map$.next(map);
  }

  onLandmarkClick(val: Landmark) {
    this.leafletCenter = L.latLng(val.latitude, val.longitude);
  }
}

来源:https://stackoverflow.com/questions/50923267/bounds-is-not-valid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!