smeijer / leaflet-geosearch

A geocoding/address-lookup library supporting various api providers.
https://smeijer.github.io/leaflet-geosearch/
MIT License
1.03k stars 273 forks source link

undefined or null markers object on showResult #227

Closed abrakadobr closed 4 years ago

abrakadobr commented 4 years ago

env: libs are loaded to website with requirejs error: on clicking any search result error happens and nothing happened - map doesn't change position, no markers appear, no event 'geosearch/showlocation' triggered. this happens inside showResult method, on line 360, if "this.markers" is null or undefined (i have no idea why it is)

   showResult(result, query) {
     ....
[patch.zip](https://github.com/smeijer/leaflet-geosearch/files/4774129/patch.zip)

     const markers = Object.keys(this.markers._layers);

but this is small patch, fixing this problem, after everithing works perfect =)

abrakadobr commented 4 years ago
diff --git a/src/SearchControl.ts b/src/SearchControl.ts
index be97a77..3874111 100644
--- a/src/SearchControl.ts
+++ b/src/SearchControl.ts
@@ -357,7 +357,7 @@ const Control: SearchControl = {
     const { autoClose } = this.options;

     // @ts-ignore
-    const markers = Object.keys(this.markers._layers);
+    const markers = this.markers?Object.keys(this.markers._layers):[];
     if (markers.length >= this.options.maxMarkers) {
       // @ts-ignore
       this.markers.removeLayer(markers[0]);

patch.zip

rawhasan commented 4 years ago

I am also getting the same error. Please share if you've found a solution for this.

smeijer commented 4 years ago

It would be nice if someone can submit a pull-request with that patch. I can take a look at it later this week.

qzminski commented 4 years ago

I had the same problem on 3.0.0 but since I updated to 3.0.5 it seems to be gone.

smeijer commented 4 years ago

@rawhasan & @abrakadobr, which version do you use?

smrbts commented 4 years ago

I had the same issue, but upgraded from 3.0 to 3.0.6 as @qzminski said, fixed the problem.

smeijer commented 4 years ago

Thanks Sam!

I'm going to close this one. Please let me know if the issue persist, so I can reopen it.