Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/beyonk-group/svelte-mapbox
MapBox Map and Autocomplete components for Svelte (or Vanilla JS)
https://github.com/beyonk-group/svelte-mapbox
autocomplete geocode geocoding google-maps location mapbox mapping maps svelte
Last synced: about 1 month ago
JSON representation
MapBox Map and Autocomplete components for Svelte (or Vanilla JS)
- Host: GitHub
- URL: https://github.com/beyonk-group/svelte-mapbox
- Owner: beyonk-group
- License: mit
- Created: 2019-07-19T18:49:31.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-02-23T21:50:54.000Z (10 months ago)
- Last Synced: 2024-04-13T18:11:44.657Z (9 months ago)
- Topics: autocomplete, geocode, geocoding, google-maps, location, mapbox, mapping, maps, svelte
- Language: Svelte
- Size: 1.14 MB
- Stars: 333
- Watchers: 9
- Forks: 61
- Open Issues: 22
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
## Svelte MapBox
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com) [![svelte-v3](https://img.shields.io/badge/svelte-v3-blueviolet.svg)](https://svelte.dev) ![publish](https://github.com/beyonk-adventures/svelte-mapbox/workflows/publish/badge.svg)
Maps and Geocoding (Autocomplete) [MapBox](https://www.mapbox.com/) components in Vanilla JS (or Svelte)
* SvelteKit Ready
* 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
```
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.
```jsx
console.log(e.detail.center.lat, e.detail.center.lng) } // recentre events
options={{ scrollZoom: false }} // // add arbitrary options to the map from the mapbox api
>
// Any custom component you create or want here - see marker example
// built in Marker component
import { Map, Geocoder, Marker, controls } from '@beyonk/svelte-mapbox'
import Earthquakes from './Earthquakes.svelte' // custom component
const { GeolocateControl, NavigationControl, ScaleControl } = 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)// Define this to handle `eventname` events - see [GeoLocate Events](https://docs.mapbox.com/mapbox-gl-js/api/markers/#geolocatecontrol-events)
function eventHandler (e) {
const data = e.detail
// do something with `data`, it's the result returned from the mapbox event
}:global(.mapboxgl-map) {
height: 200px;
// sometimes mapbox objects don't render as expected; troubleshoot by changing the height/width to px
}```
### Markers
By default, markers are typical map pins to which you can pass a color property.
```jsx
```
You may also create a custom pin with the default slot.
```
### Marker Popups
By default a popup is revealed when you click a pin. It is populated with text provided in the label property.```jsx
```
To indicate interactivity, you may target the marker with some custom CSS:
```jsx
:global(.mapboxgl-marker){
cursor: pointer;
}```
Optionally, disable the popup with the `popup={false}` property:
```jsx
```
You may alternatively pass a custom DOM element to the marker to use as a popup.
```jsx
```
### Reactive Properties
The map has reactive properties for `center` and `zoom`. This means that if you set these properties, or modify them whilst the map is displayed, the map will react accordingly.
This also means that if you bind these properties to a variable, that variable will automatically be updated with the current `center` and `zoom` of the map if the user moves or zooms the map.
This is often easier than waiting for events such as `recentre` or `zoom` to be fired, to update markers and similar:
```jsx
let center
let zoom$: lng = center[0]
$: lat = center[1]```
### Methods
The map has a variety of methods which delegate to a queue. The reason for this is that MapBox is quite a heavy library, and rendering a map is a pretty heavy operation. It's hard to guarantee
when everything is ready in your browser, and when you can start doing things with it.In case you want raw map access to interact directly with the map, you can call `getMap` on the map and interact with it that way. However we don't recommend it, as you have no guarantees that the
map is ready in your browser when you call it this way.## Basic Usage (Geocoder)
The Geocoder is an autocompleting place lookup, which returns a lat and lng for a place.
```jsx
import { Geocoder } from '@beyonk/svelte-mapbox'
```
The geocoder has five events you can subscribe to: `on:loading`, `on:result`, `on:results`, `on:clear`, and `on:error` which are [documented here](https://github.com/mapbox/mapbox-gl-geocoder/blob/master/API.md#on)
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.
## Custom CSS
You can add additional css to override mapbox provided CSS by passing the `customStylesheetUrl` property to either the `Map` or `Geocoder` components.
## Demo
To see the earthquakes demo:
Make sure you copy the `.env` file to `.env.local` and then populate it with your mapbox key.
`
npm run dev
`