calvinmetcalf / leaflet.shapefile

Shapefile in Leaflet
http://calvinmetcalf.github.io/leaflet.shapefile/
MIT License
259 stars 119 forks source link

How to install this leaflet module using npm? #53

Open jcpatac opened 6 years ago

jcpatac commented 6 years ago

So I want to use this module for my leaflet map but I'm developing it on ionic. How can I use this module?

calvinmetcalf commented 6 years ago

copy over the shp.js file leaflet.shapefile.js files into your vendors folder, or really just any folder in your app and then require them (require shp first) this was written way before I know about bundlers or anything

jcpatac commented 6 years ago

Okay, so I did just that. Inside shp.js, in the binaryAjax function, I also tweaked the ajax.addEventListener because it did not trigger or something like that. So I changed it into ajax.onreadystatechange. The logic/function is still the same but I still got an error. The error is on return resolve(new Buffer(ajax.response)); It seems like the response is null. Any thoughts on this? image

calvinmetcalf commented 6 years ago

change new Buffer(ajax.response) to Buffer.from(ajax.response)

calvinmetcalf commented 6 years ago

more generally the shapefile to geojson library is available on npm as shpjs and should work, it's only this leaflet plugin that isn't working, you might be better off just using shpjs directly and then using the built in leaflet geojson stuff.

jcpatac commented 6 years ago

So I changed new Buffer(ajax.response) to Buffer.from(ajax.response) but the same error occurred. When I log the response value to the console, it says null. I don't know why though. I think it's because I'm fetching the file locally? I am also in a localhost development server.

calvinmetcalf commented 6 years ago

yup local would be the issue, run a server so it's on local host npm install -g server && serve will do the trick

jcpatac commented 6 years ago

I already did this and the error still occurred. I don't know what went wrong really

calvinmetcalf commented 6 years ago

as I said above, this shapefile plugin for leaflet is pretty old, you might be better off using the shapefile to geojson library directly