Using leaflet-d3 on svelte

杀马特。学长 韩版系。学妹 提交于 2021-01-27 18:57:45

问题


i have to use leaflet-d3 on svelte to create a hexmap. I'm using a code already tested for other stuff. It can be found here.

function createMap(id, config, zoomFunction) {

let initialView = [config.startLat, config.startLng];
if(!config.startLat || !config.startLng){
  initialView = [0,0]
  config.startZoom = 1;
}
let m = L.map(id, {preferCanvas: true, minZoom: config.minZoom, maxZoom: config.maxZoom }).setView(initialView, config.startZoom);
let layers = {
  'Street map': L.tileLayer.provider('OpenStreetMap.Mapnik',{
    maxZoom: 36
  }),
  'Terrain map': L.tileLayer.provider('Esri.WorldImagery',{
    maxZoom: 36
  }),
  'Google Satellite': L.tileLayer('//{s}.google.com/vt/lyrs=s&x={x}&y={y}&z={z}',{
    maxZoom: 36,
    subdomains:['mt0','mt1','mt2','mt3']
  }),
  'Google Street': L.tileLayer('//{s}.google.com/vt/lyrs=m&x={x}&y={y}&z={z}',{
    maxZoom: 36,
    subdomains:['mt0','mt1','mt2','mt3']
  }),
  'Google Hybrid': L.tileLayer('//{s}.google.com/vt/lyrs=y&x={x}&y={y}&z={z}',{
    maxZoom: 36,
    subdomains:['mt0','mt1','mt2','mt3']
  })
};

if(!config.defaultMap) config.defaultMap = 'Street map';

layers[config.defaultMap].addTo(m);

if(config.showMinimap == true){
  let minimapLayer = new L.tileLayer.provider('OpenStreetMap.Mapnik',{
    maxZoom: 13
  });
  let minimap = new L.Control.MiniMap(minimapLayer, {toggleDisplay: true}).addTo(m);
}

L.control.layers(layers).addTo(m);

if(config.showScale == true){
  let scaleObject = L.control.scale({"metric":true, "imperial":false});
  scaleObject.addTo(m);
}

let hexbinLayer = L.hexbinLayer().addTo(map);




return m;
}

The problem is when i use

let hexbinLayer = L.hexbinLayer().addTo(map);

it says

leafletSrc.hexbinLayer is not a function

I imported the library like:

  import * as d3 from 'd3';
  import L from 'leaflet';

And on the index.html i imported, using examples:

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.16.0/d3.min.js" integrity="sha512-FHsFVKQ/T1KWJDGSbrUhTJyS1ph3eRrxI228ND0EGaEp6v4a/vGwPWd3Dtd/+9cI7ccofZvl/wulICEurHN1pg==" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/d3-hexbin@0.2.2/build/d3-hexbin.min.js" integrity="sha256-ikdJu86qkbPypZwSAqs06fEeiNYpdjwHWxXYbbrCeGY=" crossorigin="anonymous"></script>



<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.6.0/leaflet.js" integrity="sha512-gZwIG9x3wUXg2hdXF6+rVkLF/0Vi9U8D2Ntg4Ga5I5BZpVkVxlJWbSQtXPSiUTtC0TjtGOmxa1AJPuV0CPthew==" crossorigin="anonymous"></script>

<script src="https://cdn.jsdelivr.net/npm/@asymmetrik/leaflet-d3@4.4.0/dist/leaflet-d3.min.js" integrity="sha256-vbhDr00YfmTSp2F0FT7sIVKE56+ANHBpdW51fs3Zbko=" crossorigin="anonymous"></script>

I think i have problem of synchronization of libraries. Some help?

------- Edit

Also i have this message on the console


回答1:


As it says in this issue, you need to somehow import leaflet-d3 even though it doesn't export anything, because it adds everything to the L object.

I achieved this by adding import "@asymmetrik/leaflet-d3"; at the beginning of your code. You may need to tell eslint to ignore imports with side effects for the line, otherwise, you should be fine. Keep in mind to import L before importing leaflet-d3!

  import { onMount } from "svelte";
  // import { createMap } from '../lib/maps/generic';
  // import {replaceNestedValues} from 'cache-store';

  import L from "leaflet";
  import "leaflet-providers";
  import "leaflet-minimap";
  import { marker } from "leaflet";
  import * as d3 from "d3";
  import * as d3hexbin from "d3-hexbin";

  import "@asymmetrik/leaflet-d3";

Now I just get the error that map is undefined, but that's at least one problem solved :)



来源:https://stackoverflow.com/questions/63557422/using-leaflet-d3-on-svelte

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