[API] how toadd a json layer

Created on 23 January 2014, over 10 years ago
Updated 23 October 2023, 11 months ago

I'm having troubles to add a geojson layer to a map.
I've a geojson created by views_geojson, it's available on path: http://mysite.local/geojson

How should I add it on a map created by code?
README.txt says:

$features = array(  
  array(
    'type' => 'json',
    'json' => [JSON OBJECT],
    'properties' = array(
      'style' => [style settings],
      'leaflet_id' => 'some unique ID'
    )
  )
);

How should I load my geojson? With a simple file_get_contents it doesn't work.
Which format should have the "style settings" array?

Thanks a lot,
Sergio

PS: is it very difficult to find documentation about API for this module

💬 Support request
Status

Fixed

Version

10.0

Component

Code

Created by

🇮🇹Italy arrubiu

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇫🇷France B2F

    What is the proper way to address this issue 6 years later in d10 ? Since the latest update is breaking backward compatibility with json layer in our case ...

  • Status changed to Fixed 11 months ago
  • 🇮🇹Italy itamair

    this looks a very old (almost 10 years) issue ... not resolved in Leaflet v7 itself.

    Anyway here are the proper approaches on this:

    OPTION 1
    You want to manage the GeoJson content in Drupal,
    you need to convert Geojson into WKT format as first step,
    then generate a Node/Entity with it, & then you can do what you want with it ... such rendering it through the Leaflet Fields Formatter or the Leaflet View, or whatever (also fetching that content and rendering it in any Drupal page/route & controller).

    For converting it into a WKT format please refer to the geoPHP library that is a dependency of the Geofiled module (dependency of Leaflet itself).
    So from its Readme.md file take advantage (in your way of the following:

    $json = 
    '{
       "type": "MultiPoint",
       "coordinates": [
           [100.0, 0.0], [101.0, 1.0]
       ]
    }';
    
    $multipoint = geoPHP::load($json, 'json');
    $multipoint_points = $multipoint->getComponents();
    $wkt = $multipoint->out('wkt');

    Then something like:
    $node->set('field_geofield', $wkt);

    Ref: https://www.drupal.org/project/geofield/issues/2708529

    PS: note you will be able to import into Drupal (and take advantage) only the geometries properties in the Geojson content, as the Geofield only supports those ... (and you will miss all the others properties potentially associated with each Geojson data/feature)

    OPTION 2
    If you don't need to manage the GeoJson content in Drupal, but only add to a Leaflet map generated with this Leaflet module then you should refer to this related issues guide: https://www.drupal.org/project/leaflet/issues/3382076 💬 Adding GeoJson resource into a Drupal Leaflet Map View Needs review

    Hope 8and confident) this helps ...

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024