🏠 back to Observable

need better approch to load geojson data


#1

HI

My main difficulty is too load properly the geojson data … I would like this behavior : as long as the visitor dont click on province layer we dont load the district data if he click we check if the data are already available or need to be loaded…

https://beta.observablehq.com/@jipexu/china-cavehttps://beta.observablehq.com/@jipexu/china-cave

Sorry to be a little bit lost with this …

Any help welcome

thanks


#2

Here’s a very rough edit showing one possible approach. When the user clicks on the provinces, d3.json is called on the URL and the result is cached in a Map object:

I just wrote the caching logic into the ‘click’ callback function, but it probably makes sense to separate that out. Perhaps there are even better ways to do this.


#3

Thank bgchen for this approch
The logic seems good but in your modification i get this error
urlObj = TypeError: Object.fromEntries is not a function. (In 'Object.fromEntries(urls.map(url => [url.match(/\/([a-z]{2,3})\.json/)[1],url]))', 'Object.fromEntries' is undefined)

i go to investigate this many thanks


#4

Ah, oops! That’s a bleeding-edge Firefox-only feature (so far). I’ve replaced it with Map in the latest version (it’s better to use Map here anyways):


#5

Many Thanks !
I implementing now … I can not merge directly because i am not familliar with this tool (and had make a lot of modification since the fork …).

This seems to me very good …and work well !