Problem/Motivation
Using leaflet service provided by the module to display maps containing geojson feature, the javascript is crashing while attempting to create the json feature because a reference to this is wrong when it is trying to call feature_bind_tooltip and feature_bind_popup on the object hosting the function.
Uncaught TypeError: this.feature_bind_tooltip is not a function
onEachFeature leaflet.drupal.js:872
addData GeoJSON.js:128
create_json leaflet.drupal.js:884
create_geometry leaflet.drupal.js:530
create_feature leaflet.drupal.js:556
add_features leaflet.drupal.js:381
Steps to reproduce
Create a map containing geoJson features and render them with leaflet service's leafletRenderMap method.
Proposed resolution
Fix the reference to "this" in Drupal.Leaflet.prototype.create_json, currently it is pointing inside the function lJSON.options.onEachFeature itself instead of the parent's.
Remaining tasks
Review & apply
User interface changes
None
API changes
None
Data model changes
None
| Comment | File | Size | Author |
|---|---|---|---|
| #13 | Drupal Leaflet GeoJson Approach Description.jpg | 467.07 KB | itamair |
| #2 | create_json-feature-maps-this-reference-fix-3377403-1.patch | 863 bytes | b2f |
Comments
Comment #2
b2f commentedComment #3
b2f commentedComment #4
b2f commentedComment #5
b2f commentedComment #6
ressaThanks for working on this. I see a similar syntax for "Leaflet Feature (Point or Geometry)". Perhaps you can check if it should also be updated? https://git.drupalcode.org/project/leaflet/-/blob/10.0.x/js/leaflet.drup...
Comment #7
b2f commentedI don't think calls to this in Drupal.Leaflet.prototype.create_feature are an issue since they are called at the prototype's function level. Furthermore I would probably have seen the error also there if it was necessary to modify create_feature, my patch alone seems to resolve the issue mentioned in the description.
Comment #8
ressaThanks for checking @b2f, it's much appreciated.
Comment #9
itamair commentedthanks @b2f for reporting this.
But the Drupal.Leaflet.prototype.create_json method/property was really never validated/used/debugged since I started maintaining the Leaflet module (for D8), and I think there could be more issues in its implementation, than the once you point with your patch.
I don't exactly get when you write:
How are you doing that?
Because I am not aware of any WKT definition with "json" type ...
It would be very interesting for me to understand how to re-create your use case,
but I don't know how to generate it, and have here: https://git.drupalcode.org/project/leaflet/-/blob/10.0.x/js/leaflet.drup...
a
featurethat hasfeature.type == "json".Could you provide all the code you are using in this issue context?
Or at least the the whole
featureobject definition, so that I could inject in the js console workflow and recreate your use case and debug and (potentially) fix all theDrupal.Leaflet.prototype.create_jsonfunction?Thanks a lot ...
Comment #10
b2f commentedHello itamair,
I made a wrapper around leafmet.service with the following method:
The $geoJsons array is containing typical geojson strings as such:
"{"type": "Feature", "properties": { "scalerank": 1, "featurecla": "Admin-0 country", "labelrank": 2.0, "sovereignt": "Argentina", "sov_a3": "ARG", "adm0_dif": 0.0, "level": 2.0, "type": "Sovereign country", "admin": "Argentina", "adm0_a3": "ARG", "geou_dif": 0.0, "geounit": "Argentina", "gu_a3": "ARG", "su_dif": 0.0, "subunit": "Argentina", "su_a3": "ARG", "brk_diff": 0.0, "name": "Argentina", "name_long": "Argentina", "brk_a3": "ARG", "brk_name": "Argentina", "brk_group": null, "abbrev": "Arg.", "postal": "AR", "formal_en": "Argentine Republic", "formal_fr": null, "note_adm0": null, "note_brk": null, "name_sort": "Argentina", "name_alt": null, "mapcolor7": 3.0, "mapcolor8": 1.0, "mapcolor9": 3.0, "mapcolor13": 13.0, "pop_est": 40913584.0, "gdp_md_est": 573900.0, "pop_year": -99.0, "lastcensus": 2010.0, "gdp_year": -99.0, "economy": "5. Emerging region: G20", "income_grp": "3. Upper middle income", "wikipedia": -99.0, "fips_10": null, "iso_a2": "AR", "iso_a3": "ARG", "iso_n3": "032", "un_a3": "032", "wb_a2": " [...]Looks like it was taken straight out of a dataset such as: https://github.com/datasets/geo-boundaries-world-110m/blob/master/countr...
Then the map is displayed in a typical controller through the method above. Not sure if you need more info about the map settings for example ?
Comment #11
b2f commentedComment #12
itamair commentedthanks @b2f for you detailed feedback.
I reproduced your use case (that looks very specific and arbitrary to me, as not documented and advised anywhere ...) and I could somehow forced the adjustment of the Drupal.Leaflet.prototype.create_json method/function,
BUT I really found myself in a not existing use case for the definition of Geofield features types (json type doesn't exists in WKT formats ... ).
Hence I am going to completely remove it from next Leaflet module release.
As it looks you are "simply" trying to inject GeoJson resources in a Drupal Leaflet Map View, I strongly suggest to refer to (and follow) this Leaflet Support issue I created both for this purpose and for coping more general users need: Adding GeoJson resource into a Drupal Leaflet Map View
For more detailed information and guide on this, please refer to the following Leaflet Map GeoJson Demo page, and related instructions / insights ...
Comment #13
itamair commentedComment #14
itamair commentedComment #15
b2f commentedLeaflet Map View as in Views module ?
Sorry but no we were not using the Views module, as described in my previous comment I explained "The map is displayed in a typical controller through the method above".
Comment #16
itamair commentedWell, it doesn’t change anything.
Whichever is the way you generate the Drupal Leaflet map you could (and should) use that approach to add/inject GeoJson resources into that …
Comment #18
b2f commentedCould you please explain where to go from the above because your d10 update breaks the geojson layers in our case.
Can you kindly please be more specific with ?
?
I mean can't you see how much we were apparently relying on this feature ?
It's like if I got punished to open this issue because you wouldn't have seen this in the first place ? Come on' ...
Comment #19
b2f commentedDoesn't it breaks https://www.drupal.org/project/leaflet/issues/3186029 ?
Comment #20
b2f commentedComment #21
b2f commentedComment #22
itamair commented@b2f ok ... probably I was too quick in simply removing this Drupal.Leaflet.prototype.create_json, just because I couldn't figure out any valid use case for this, and couldn't get your successfully way in doing that.
Still I hope you could give better evidence of it (may be with a more detailed description, with some code Gists i.e.)and thus give a practical contribution and guidance to the Drupal Leaflet module community ...
Anyway you persuaded me to rollback the deletion of the Drupal.Leaflet.prototype.create_json method and apply (a slightly different version of) your #2 patch, but still crediting you
(no body wants to punish anybody ... may be it was just some misunderstanding).
The following commits:
https://git.drupalcode.org/project/leaflet/-/commit/59012fdb03e47354da02...
https://git.drupalcode.org/project/leaflet/-/commit/dd342f17ea68a8499136...
https://git.drupalcode.org/project/leaflet/-/commit/09f21141549c5a5604bb...
are now into the 10.0.x-dev branch and are going to be part of the incoming 10.2.2 new Leaflet module release.
Comment #23
b2f commentedThanks for listening, because, actually until now I just applied a patch myself to rollback the changes, otherwise it would have needed a huge rewrite.
I already specifically asked you if you needed more informations about my implementation in comment #10, https://www.drupal.org/project/leaflet/issues/3377403#comment-15190179
The thing is there is not a single line of custom JS nor the need to inject a Drupal setting in this case, neither the use of Views module. It's just a controller which returns a standard render array, and it works as is with the leafletRenderMap from the leaflet service, I thought it was clear from my code snippet ... What is missing from my example above ?