简体   繁体   中英

LeafletJS invalid GeoJSON object

I'm working with Leaflet trying to get some GeoJSON going. Everything fine, map is working, but in DevTools console i have an error with invalid GeoJSON. My component is:

I have getRegionsGeoJson, which return to me geoJSON from some url.

My DevTools Console

ngOnInit() {
    this.regionsGeoJsonSubscription$ = this.mapService.getRegionsGeoJson()
      .subscribe(regionsGeoJson => {
          this.regionsGeoJson = regionsGeoJson;
          this.regionsGeoJsonLoaded = Object.keys(this.regionsGeoJson).length > 0;
          this.statisticsEnabled = true;
          this.hidePreloader();
        }
      );

    // LeafletJS map init
    this.map = L.map('map')
      .setView([-25.441105, -49.276855], 13);

    L.tileLayer
      .provider('OpenStreetMap.Mapnik')
      .addTo(this.map);

    L.geoJSON( {
      style: function (feature) {
        return {color: feature.properties.color};
      }
    }).bindPopup(function (layer) {
      return layer.feature.properties.description;
    }).addTo(this.map);

  }
  hidePreloader() {
    this.preloader.nativeElement.style.display = 'none';
  }
}

L.geoJSON factory expects its first argument to be your GeoJSON data, or null if you will be providing it later with addData method.

The error you see is because it tries to interpret your optuons object as a GeoJSON objetc, and obviously fails.

Solution:

L.geoJSON(this.regionsGeoJson <-- solution {
  style: function (feature) {
    return {color: feature.properties.color};
  }
}).bindPopup(function (layer) {
  return layer.feature.properties.description;
}).addTo(this.map);

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