Add GeoJSON data
Data appears on the map through two style objects. A source contains the GeoJSON, and a layer draws its features. Add both after a style-loaded event. Repeat the additions after every later style load, as Load a style describes.
Choose the data form and the options
Section titled “Choose the data form and the options”A source reads its data from a URL or from host memory. Use
mln_map_add_geojson_source_url for data that a server publishes. MapLibre
fetches the URL through its resource system and reloads it when requested. Use
mln_map_add_geojson_source_data for data that the host computes or receives
through its own transport. Inline data is prepared first:
mln_geojson_source_data_create parses one complete UTF-8 GeoJSON value and
tiles it, and the add installs the prepared result.
Choose the source options at creation. A URL source takes the options in the add call, and an inline source adopts the options that its data was prepared with. Changing them requires removing every layer that draws the source, replacing the source, and adding the layers again. A clustering source groups nearby points into cluster features. Its options set the cluster radius, maximum clustering zoom, and minimum cluster size.
// Each value applies when its field bit is set.mln_geojson_source_options options = mln_geojson_source_options_default();options.fields |= MLN_GEOJSON_SOURCE_OPTION_CLUSTER | MLN_GEOJSON_SOURCE_OPTION_CLUSTER_RADIUS;options.cluster = true;options.cluster_radius = 60; // pixelsAdd the source and a layer
Section titled “Add the source and a layer”Pass the URL and the options to the call that adds the source. The layer names the same source ID.
mln_status status = mln_map_add_geojson_source_url( map, view("earthquakes"), view(geojson_url), &options);if (status != MLN_STATUS_OK) return status;The layer is a style-spec layer object with at least an id, a type, and the
source ID. Choose the type that matches your geometry: a circle layer draws
points, a line layer draws line strings, and a fill layer draws polygons.
const char layer[] = "{\"id\":\"earthquake-circles\",\"type\":\"circle\"," "\"source\":\"earthquakes\"}";Drawing order follows the style. Append the layer to draw it above the existing layers, or name an existing layer to insert the new layer beneath it.
return mln_map_add_style_layer_json(map, view(layer), view(""));Supply the data from memory
Section titled “Supply the data from memory”Inline data uses a complete GeoJSON value produced by the host’s serializer. Preparation parses and tiles the value, runs on any thread, and returns a handle that installs on any number of sources. Destroy the handle after the installs; each source keeps its own reference. A clustering source requires a feature collection in which every feature has point geometry. Other shapes report an invalid-argument status when the data is prepared.
char data[256];const int length = snprintf( data, sizeof(data), "{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\"," "\"geometry\":{\"type\":\"Point\",\"coordinates\":[%.8f,%.8f]}," "\"properties\":{}}]}", position.longitude, position.latitude);if (length < 0 || (size_t)length >= sizeof(data)) { return MLN_STATUS_INVALID_ARGUMENT;}
// A null options pointer selects the defaults. Clustering is off by default.mln_geojson_source_data prepared = MLN_HANDLE_NULL;mln_status status = mln_geojson_source_data_create(view(data), NULL, &prepared);if (status != MLN_STATUS_OK) return status;
status = mln_map_add_geojson_source_data(map, view("pins"), prepared);mln_geojson_source_data_destroy(prepared);return status;A URL source parses and tiles on worker threads after the fetch. Update data covers replacing inline data as it changes.