mapboxglgojs

package module
v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 9, 2025 License: MIT Imports: 14 Imported by: 0

README

Mapbox GL GOJS

Interactive map and geo visualization for Golang. Generate Mapbox HTML.

Docs + Examples https://visendi-labs.github.io/mapbox-gl-gojs/#/

Usage - Generate Mapbox HTML/JS from Go
mapbox := mbgojs.NewScript(
	mbgojs.NewMap(mbgojs.Map{
		Container:   "map",
		AccessToken: "<MAPBOX_ACCESS_TOKEN>",
		Config:      mbgojs.MapConfig{Basemap: mbgojs.BasemapConfig{Theme: "faded"}},
	}),
)

mapbox.MustRenderDefault() // <script>const map = new mapboxgl.Map({container: "map", ...});</script>
points := geojson.NewFeatureCollection()
points.Append(geojson.NewFeature(orb.Point{rand.Float64() * 50, rand.Float64() * 50}))

event := mbgojs.NewMapOnLoad(
	mbgojs.NewMapAddLayer(mbgojs.MapLayer{
		Id: "layer", Type: "line",
		Source: mbgojs.MapSource{Type: "geojson", Data: *points},
	}),
)

event.MustRenderDefault() // map.on("load", (e) => {map.addLayer({ ... })});

Documentation

Index

Constants

This section is empty.

Variables

View Source
var HtmxAjaxContextEventValuesFull map[string]string = map[string]string{
	"eventType": "e.type",
	"lat":       "e.lngLat.lat",
	"lng":       "e.lngLat.lng",
	"featureId": "e.features[0].id",
	"layerId":   "e.features[0].layer.id",
	"layerType": "e.features[0].layer.type",
	"sourceId":  "e.features[0].source",
	"type":      "e.features[0].type",
	"x":         "e.point.x",
	"y":         "e.point.y",
}
View Source
var HtmxAjaxContextEventValuesNoFeature map[string]string = map[string]string{
	"eventType": "e.type",
	"lat":       "e.lngLat.lat",
	"lng":       "e.lngLat.lng",
	"x":         "e.point.x",
	"y":         "e.point.y",
}

Functions

This section is empty.

Types

type BasemapConfig

type BasemapConfig struct {
	Theme                  string `json:"theme,omitempty"`
	Show3DObjects          bool   `json:"show3dObjects,omitempty"`
	ShowPointOfInterest    bool   `json:"showPointOfInterestLabels,omitempty"`
	ShowPlaceLabels        bool   `json:"showPlaceLabels,omitempty"`
	ShowRoadLabels         bool   `json:"showRoadLabels,omitempty"`
	ShowTransitLabels      bool   `json:"showTransitLabels,omitempty"`
	ShowAdminBoundaries    bool   `json:"showAdminBoundaries,omitempty"`
	ShowBuildingExtrusions bool   `json:"showBuildingExtrusions,omitempty"`
	ShowRoads              bool   `json:"showRoads,omitempty"`
	ShowTransit            bool   `json:"showTransit,omitempty"`
	ShowTerrain            bool   `json:"showTerrain,omitempty"`
	ShowWater              bool   `json:"showWater,omitempty"`
	LightPreset            string `json:"lightPreset,omitempty"`
}

type CameraOptions

type CameraOptions struct {
	Around         orb.Point      `json:"around,omitempty"`
	Bearing        float64        `json:"bearing,omitempty"`
	Center         orb.Point      `json:"center,omitempty"`
	Pitch          float64        `json:"pitch,omitempty"`
	PaddingOptions PaddingOptions `json:"paddingOptions,omitempty"`
	RetainPadding  bool           `json:"retainPadding,omitempty"`
	Zoom           float64        `json:"zoom,omitempty"`
}

type EnclosedSnippetCollection

type EnclosedSnippetCollection struct {
	Template string
	Data     any
	Children []EnclosedSnippetCollectionRenderable
}

type EnclosedSnippetCollectionRenderable

type EnclosedSnippetCollectionRenderable func(RenderConfig) *EnclosedSnippetCollection

func NewHtmxAjaxRaw

func NewHtmxAjaxRaw(verb, path, data string) EnclosedSnippetCollectionRenderable

func NewMapAddImage

func NewMapAddImage(name string, image image.Image) EnclosedSnippetCollectionRenderable

func NewMapAddImageBase64

func NewMapAddImageBase64(name, imgBase64 string, height, width int) EnclosedSnippetCollectionRenderable

func NewMapAddImageCircle

func NewMapAddImageCircle(name string, rad int, border float64, borderColor, circleColor color.RGBA) EnclosedSnippetCollectionRenderable

func NewMapAddImageRectangle

func NewMapAddImageRectangle(name string, height, width, border int) EnclosedSnippetCollectionRenderable

func NewMapAddSource

TODO: Make this stricter/break out into more than one? Different types/formats of sources

func NewMapOnEventHtmxAjax

func NewMapOnEventHtmxAjax(event string, data HtmxAjax) EnclosedSnippetCollectionRenderable

func NewMapOnEventLayer

map.on("event", layer, (e) => { ... });

func NewMapOnEventLayerCursor

func NewMapOnEventLayerCursor(event, layer, cursor string) EnclosedSnippetCollectionRenderable

func NewMapOnEventLayerHtmxAjax

func NewMapOnEventLayerHtmxAjax(event, layer string, data HtmxAjax) EnclosedSnippetCollectionRenderable

TODO: Be able to pass data down into children? Messy?

func NewMapOnEventLayerHtmxAjaxEventData

func NewMapOnEventLayerHtmxAjaxEventData(event, layer, verb, path string, h HtmxAjaxContext) EnclosedSnippetCollectionRenderable

TODO: Remove this!?!? Just better off using the NewMapOnEventLayer instead NOTE: This one needs to be inside an event, so that it has access to the event variable e

func NewMapOnEventLayerPairFeatureState

func NewMapOnEventLayerPairFeatureState(event1, event2, layer, source, feature, event1Value, event2Value string) EnclosedSnippetCollectionRenderable

TODO: This is probably way more sane than to have a HTMX endpoint do the same thing? Or? TODO: Could this be more modular? Or is it good to have a super specific function like this? TODO: Make the input here a struct instead?

func NewMapRemoveLayer

func NewMapRemoveLayer(layerId string) EnclosedSnippetCollectionRenderable

func NewMapRemoveSource

func NewMapRemoveSource(sourceId string) EnclosedSnippetCollectionRenderable

func NewMapSetFeatureState

func NewMapSetFeatureState(source, sourceLayer, id string, features map[string]string) EnclosedSnippetCollectionRenderable

TODO: We really need to make it clear which values are not wrapped in quotes? General structs with different fields?

func NewMapSetLayoutProperty

func NewMapSetLayoutProperty(layerId, propertry, value string) EnclosedSnippetCollectionRenderable

TODO: For hover things - generate a UUID variable name to use for keeping track of "hovered line" id? TODO: This can't set other values than string atm

func NewMapSourceSetDataFromLayer

func NewMapSourceSetDataFromLayer(layerId string, data any) EnclosedSnippetCollectionRenderable

Sould this just use a setData from source id?

func NewMapSourceUpdateImageUrl

func NewMapSourceUpdateImageUrl(id string, url string) EnclosedSnippetCollectionRenderable

func NewMapboxDraw

func NewMapboxDraw(mapboxDrawConfig MapboxDrawConfig) EnclosedSnippetCollectionRenderable

func NewPopup

TODO add all options in Popup constructor

func (EnclosedSnippetCollectionRenderable) MustRender

func (EnclosedSnippetCollectionRenderable) MustRenderDefault

func (esc EnclosedSnippetCollectionRenderable) MustRenderDefault() string

func (EnclosedSnippetCollectionRenderable) Render

type FitBoundsOptions

type FitBoundsOptions struct {
	Bearing float64 `json:"bearing,omitempty"`
	Linear  bool    `json:"linear,omitempty"`
	MaxZoom float64 `json:"maxZoom,omitempty"`
	Padding float64 `json:"padding,omitempty"`
	Pitch   float64 `json:"pitch,omitempty"`
}

type FlyToOptions

type FlyToOptions struct {
	Curve       float64 `json:"curve,omitempty"`
	MaxDuration float64 `json:"maxDuration,omitempty"`
	MinZoom     float64 `json:"minZoom,omitempty"`
	ScreenSpeed float64 `json:"screenSpeed,omitempty"`
	Speed       float64 `json:"speed,omitempty"`
}

type HtmxAjax

type HtmxAjax struct {
	Path    string
	Verb    string
	Context HtmxAjaxContext
}

type HtmxAjaxContext

type HtmxAjaxContext struct {
	Headers map[string]string
	Values  map[string]string
	Swap    string
	Target  string
	Event   string
	Handler string
	Select  string
	Source  string
}

type Map

type Map struct {
	// Required
	Container   string    `json:"container,omitempty"`       // HTML element id or reference
	Style       string    `json:"style,omitempty"`           // Style URL or JSON object
	Center      orb.Point `json:"center,omitempty,omitzero"` // [lng, lat]
	Zoom        float64   `json:"zoom,omitempty,omitzero"`
	AccessToken string    `json:"accessToken,omitempty"`

	// Optional view state
	Bearing          float64   `json:"bearing,omitempty,omitzero"` // Map rotation in degrees
	Pitch            float64   `json:"pitch,omitempty,omitzero"`   // Tilt in degrees
	Bounds           []float64 `json:"bounds,omitempty,omitzero"`  // [minX, minY, maxX, maxY]
	FitBoundsOptions any       `json:"fitBoundsOptions,omitempty,omitzero"`

	// Interaction controls
	Interactive     bool `json:"interactive,omitempty"`
	ScrollZoom      bool `json:"scrollZoom,omitempty"`
	DragRotate      bool `json:"dragRotate,omitempty"`
	DragPan         bool `json:"dragPan,omitempty"`
	Keyboard        bool `json:"keyboard,omitempty"`
	DoubleClickZoom bool `json:"doubleClickZoom,omitempty"`
	TouchZoomRotate bool `json:"touchZoomRotate,omitempty"`

	// Render/Performance options
	MinZoom                      float64   `json:"minZoom,omitempty,omitzero"`
	MaxZoom                      float64   `json:"maxZoom,omitempty,omitzero"`
	MaxBounds                    []float64 `json:"maxBounds,omitempty,omitzero"` // [minX, minY, maxX, maxY]
	PreserveDrawingBuffer        bool      `json:"preserveDrawingBuffer,omitempty"`
	Antialias                    bool      `json:"antialias,omitempty"`
	TrackResize                  bool      `json:"trackResize,omitempty"`
	FailIfMajorPerformanceCaveat bool      `json:"failIfMajorPerformanceCaveat,omitempty"`

	// Locale and attribution
	Locale             map[string]string `json:"locale,omitempty"`
	AttributionControl bool              `json:"attributionControl,omitempty"`

	// Misc
	Hash bool `json:"hash,omitempty"` // Track position in URL hash

	// Your custom config
	Config MapConfig `json:"config,omitempty,omitzero"`
}

type MapConfig

type MapConfig struct {
	Basemap BasemapConfig `json:"basemap,omitempty,omitzero"`
}

type MapLayer

type MapLayer struct {
	Id            string         `json:"id,omitempty"`
	Type          string         `json:"type,omitempty"`
	Source        any            `json:"source,omitempty"`
	SourceLayer   string         `json:"sourceLayer,omitempty"`
	Layout        MapLayout      `json:"layout,omitempty"` // TODO varies with type (any here?)
	Slot          string         `json:"slot,omitempty"`
	BeforeId      string         `json:"beforeId,omitempty"`
	Paint         MapLayerPaint  `json:"paint,omitempty"`
	MinZoom       float64        `json:"minzoom,omitempty"`
	MaxZoom       float64        `json:"maxzoom,omitempty"`
	Metadata      map[string]any `json:"metadata,omitempty"`
	Filter        any            `json:"filter,omitempty"`
	RenderingMode string         `json:"renderingMode,omitempty"`
}

type MapLayerPaint

type MapLayerPaint struct {
	// Common
	Opacity any `json:"opacity,omitempty"`

	// Fill layers
	FillColor        any    `json:"fill-color,omitempty"`
	FillOpacity      any    `json:"fill-opacity,omitempty"`
	FillOutlineColor any    `json:"fill-outline-color,omitempty"`
	FillPattern      string `json:"fill-pattern,omitempty"`

	// Line layers
	LineColor     any    `json:"line-color,omitempty"`
	LineOpacity   any    `json:"line-opacity,omitempty"`
	LineWidth     any    `json:"line-width,omitempty"`
	LineGapWidth  any    `json:"line-gap-width,omitempty"`
	LineOffset    any    `json:"line-offset,omitempty"`
	LineDashArray any    `json:"line-dasharray,omitempty"`
	LinePattern   string `json:"line-pattern,omitempty"`

	// Symbol layers
	IconOpacity   any `json:"icon-opacity,omitempty"`
	IconColor     any `json:"icon-color,omitempty"`
	IconHaloColor any `json:"icon-halo-color,omitempty"`
	IconHaloWidth any `json:"icon-halo-width,omitempty"`
	IconHaloBlur  any `json:"icon-halo-blur,omitempty"`
	TextOpacity   any `json:"text-opacity,omitempty"`
	TextColor     any `json:"text-color,omitempty"`
	TextHaloColor any `json:"text-halo-color,omitempty"`
	TextHaloWidth any `json:"text-halo-width,omitempty"`
	TextHaloBlur  any `json:"text-halo-blur,omitempty"`

	// Circle layers
	CircleRadius        any `json:"circle-radius,omitempty"`
	CircleColor         any `json:"circle-color,omitempty"`
	CircleOpacity       any `json:"circle-opacity,omitempty"`
	CircleStrokeColor   any `json:"circle-stroke-color,omitempty"`
	CircleStrokeWidth   any `json:"circle-stroke-width,omitempty"`
	CircleStrokeOpacity any `json:"circle-stroke-opacity,omitempty"`

	// Raster layers
	RasterOpacity       any `json:"raster-opacity,omitempty"`
	RasterHueRotate     any `json:"raster-hue-rotate,omitempty"`
	RasterBrightnessMin any `json:"raster-brightness-min,omitempty"`
	RasterBrightnessMax any `json:"raster-brightness-max,omitempty"`
	RasterSaturation    any `json:"raster-saturation,omitempty"`
	RasterContrast      any `json:"raster-contrast,omitempty"`
	RasterFadeDuration  any `json:"raster-fade-duration,omitempty"`
	RasterColorMix      any `json:"raster-color-mix,omitempty"`

	// Hillshade layers
	HillshadeIlluminationDirection any    `json:"hillshade-illumination-direction,omitempty"`
	HillshadeIlluminationAnchor    string `json:"hillshade-illumination-anchor,omitempty"`
	HillshadeExaggeration          any    `json:"hillshade-exaggeration,omitempty"`
	HillshadeHighlightColor        any    `json:"hillshade-highlight-color,omitempty"`
	HillshadeShadowColor           any    `json:"hillshade-shadow-color,omitempty"`
	HillshadeAccentColor           any    `json:"hillshade-accent-color,omitempty"`

	// Background layers
	BackgroundColor   any    `json:"background-color,omitempty"`
	BackgroundOpacity any    `json:"background-opacity,omitempty"`
	BackgroundPattern string `json:"background-pattern,omitempty"`

	// Fill-extrusion layers (3D buildings, etc.)
	FillExtrusionColor   any    `json:"fill-extrusion-color,omitempty"`
	FillExtrusionOpacity any    `json:"fill-extrusion-opacity,omitempty"`
	FillExtrusionHeight  any    `json:"fill-extrusion-height,omitempty"`
	FillExtrusionBase    any    `json:"fill-extrusion-base,omitempty"`
	FillExtrusionPattern string `json:"fill-extrusion-pattern,omitempty"`

	// Heatmap layers
	HeatmapRadius    any `json:"heatmap-radius,omitempty"`
	HeatmapWeight    any `json:"heatmap-weight,omitempty"`
	HeatmapIntensity any `json:"heatmap-intensity,omitempty"`
	HeatmapColor     any `json:"heatmap-color,omitempty"`
	HeatmapOpacity   any `json:"heatmap-opacity,omitempty"`
}

type MapLayout

type MapLayout struct {
	// Line properties
	LineJoin string `json:"line-join,omitempty"` // miter, bevel, round
	LineCap  string `json:"line-cap,omitempty"`  // butt, round, square

	// Symbol/Icon properties
	IconImage           string `json:"icon-image,omitempty"`
	IconSize            any    `json:"icon-size,omitempty"`
	IconRotate          any    `json:"icon-rotate,omitempty"`
	IconOffset          any    `json:"icon-offset,omitempty"`
	IconAnchor          string `json:"icon-anchor,omitempty"` // center, top, bottom, left, right, etc.
	IconAllowOverlap    bool   `json:"icon-allow-overlap,omitempty"`
	IconIgnorePlacement bool   `json:"icon-ignore-placement,omitempty"`
	IconOptional        bool   `json:"icon-optional,omitempty"`

	// Text properties
	TextField           string `json:"text-field,omitempty"`
	TextFont            any    `json:"text-font,omitempty"`
	TextSize            any    `json:"text-size,omitempty"`
	TextMaxWidth        any    `json:"text-max-width,omitempty"`
	TextLineHeight      any    `json:"text-line-height,omitempty"`
	TextLetterSpacing   any    `json:"text-letter-spacing,omitempty"`
	TextJustify         string `json:"text-justify,omitempty"`
	TextAnchor          string `json:"text-anchor,omitempty"`
	TextRotate          any    `json:"text-rotate,omitempty"`
	TextTransform       string `json:"text-transform,omitempty"`
	TextOffset          any    `json:"text-offset,omitempty"`
	TextAllowOverlap    bool   `json:"text-allow-overlap,omitempty"`
	TextIgnorePlacement bool   `json:"text-ignore-placement,omitempty"`
	TextOptional        bool   `json:"text-optional,omitempty"`

	// Raster
	RasterColorRange []float64 `json:"raster-color-range,omitempty"`

	// Visibility
	Visibility string `json:"visibility,omitempty"` // visible, none
}

type MapSource

type MapSource struct {
	Type        string      `json:"type,omitempty"`
	Data        any         `json:"data,omitempty"`
	Coordinates []orb.Point `json:"coordinates,omitempty"`
	GenerateId  bool        `json:"generateId,omitempty"`
	Url         string      `json:"url,omitempty"`
}

type MapboxDrawConfig

type MapboxDrawConfig struct {
	Keybindings            bool                     `json:"keybindings,omitempty"`
	TouchEnabled           bool                     `json:"touchEnabled,omitempty"`
	BoxSelect              bool                     `json:"boxSelect,omitempty"`
	ClickBuffer            uint                     `json:"clickBuffer,omitempty"`
	TouchBuffer            uint                     `json:"touchBuffer,omitempty"`
	Controls               MapboxDrawConfigControls `json:"controls,omitempty"`
	DisplayControlsDefault bool                     `json:"displayControlsDefault,omitempty"`
	Styles                 any                      `json:"styles,omitempty"` // TODO
	Modes                  any                      `json:"modes,omitempty"`
	DefaultMode            string                   `json:"defaultMode,omitempty"`
	UserProperties         bool                     `json:"userProperties,omitempty"`
	SuppressAPIEvents      bool                     `json:"suppressAPIEvents,omitempty"`
}

type MapboxDrawConfigControls

type MapboxDrawConfigControls struct {
	Point             bool `json:"point,omitempty"`
	LineString        bool `json:"line_string,omitempty"`
	Polygon           bool `json:"polygon,omitempty"`
	Trash             bool `json:"trash,omitempty"`
	CombineFeatures   bool `json:"combine_features,omitempty"`
	UncombineFeatures bool `json:"uncombine_features,omitempty"`
}

type PaddingOptions

type PaddingOptions struct {
	Bottom float64 `json:"bottom,omitempty"`
	Left   float64 `json:"left,omitempty"`
	Right  float64 `json:"right,omitempty"`
	Top    float64 `json:"top,omitempty"`
}

type PopupConfig

type PopupConfig struct {
	Altitude       float64 `json:"altitude,omitempty"`
	Anchor         string  `json:"anchor,omitempty"`
	ClassName      string  `json:"className,omitempty"`
	CloseButton    bool    `json:"closeButton,omitempty"`
	CloseOnClick   bool    `json:"closeOnClick,omitempty"`
	CloseOnMove    bool    `json:"closeOnMove,omitempty"`
	FocusAfterOpen bool    `json:"focusAfterOpen,omitempty"`
	MaxWidth       float64 `json:"maxWidth,omitempty"`
	Offset         any     `json:"offset,omitempty"`
}

type RenderConfig

type RenderConfig struct {
	UseCustomJsonMarshal bool
	KeepWhitespace       bool
}

TODO: Add option to say what the map object should be called (mapboxgljs.Map())

type RenderConfigOption

type RenderConfigOption int
const (
	UseCustomJsonMarshal RenderConfigOption = iota
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL