svelte-mapbox

MapBox Map and Autocomplete components for Svelte (or Vanilla JS)

Github星跟踪图

Svelte MapBox

js-standard-style CircleCI svelte-v2 svelte-v3

Maps and Geocoding (Autocomplete) components in Vanilla JS (or Svelte)

  • SSR Ready

  • Lightweight

  • No clientside dependencies (Map)

  • Allow creation of custom Svelte components on the map

  • Note that the GeoCoder has a clientside dependency, since it adds about 0.5mb to the bundle size, and significant time to the build time if bundled.

Installing

It is PARAMOUNT that you install this as a development dependency, not a runtime dependency. It won't work otherwise. Svelte bundles everything, so you should need any runtime dependencies at all

npm install --save-dev @beyonk/svelte-mapbox

Basic Usage (Map)

The container component is the map, and there are a variety of components which go on the map.

<Map
  accessToken="<your api key>" // add your api key here
  bind:this={mapComponent} // Get reference to your map component to use methods
  on:recentre={e => console.log(e.detail.center.lat, e.detail.center.lng) } // recentre events
>
  <Earthquakes /> // Any custom component you create or want here - see marker example
  <Marker lat={someLat} lng={someLng} color="rgb(255,255,255)" label="some marker label" popupClassName="class-name" /> // built in Marker component
  <NavigationControl />
  <GeolocateControl options={{ some: 'control-option' }} />
  <ScalingControl />
</Map>

<script>
  import { Map, Geocoder, Marker, controls } from '@beyonk/svelte-mapbox'
	import Earthquakes from './Earthquakes.svelte' // custom component
  
  const { GeolocateControl, NavigationControl, ScalingControl } = controls

  // Usage of methods like setCenter and flyto
  mapComponent.setCenter([lng,lat],zoom) // zoom is optional
  mapComponent.flyTo({center:[lng,lat]}) // documentation (https://docs.mapbox.com/mapbox-gl-js/example/flyto)
</script>

Basic Usage (Geocoder)

The Geocoder is an autocompleting place lookup, which returns a lat and lng for a place.

<Geocoder accessToken="<your api key>" on:result={somePlaceChangeFunction} />

<script>
  import { Geocoder } from '@beyonk/svelte-mapbox'
</script>

The geocoder has five events you can subscribe to: on:loading, on:result, on:results, on:clear, and on:error which are documented here

The most important event is on:result which is fired when a user selects an autocomplete result.

There is a sixth event specific to this library, which is on:ready, which is fired when the component is ready for use. You can likely ignore it.

Demo

To see the earthquakes demo:

npm run dev

主要指标

概览
名称与所有者beyonk-group/svelte-mapbox
主编程语言Svelte
编程语言JavaScript (语言数: 4)
平台
许可证MIT License
所有者活动
创建于2019-07-19 18:49:31
推送于2025-01-22 04:28:50
最后一次提交2024-11-11 12:43:02
发布数77
最新版本名称v11.0.0 (发布于 2024-11-11 12:43:02)
第一版名称v1.0.1 (发布于 2019-07-20 14:16:20)
用户参与
星数357
关注者数7
派生数63
提交数227
已启用问题?
问题数55
打开的问题数22
拉请求数26
打开的拉请求数2
关闭的拉请求数17
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?