All Projects → beyonk-adventures → svelte-mapbox

beyonk-adventures / svelte-mapbox

Licence: MIT license
MapBox Map and Autocomplete components for Svelte (or Vanilla JS)

Programming Languages

Svelte
593 projects
CSS
56736 projects
javascript
184084 projects - #8 most used programming language
HTML
75241 projects
shell
77523 projects

Projects that are alternatives of or similar to svelte-mapbox

Benmaps.fr
Web maps that don't track you.
Stars: ✭ 147 (-44.94%)
Mutual labels:  geocoding, maps, google-maps, mapbox
Google Maps
Google Maps Web Services API wrapper for .NET
Stars: ✭ 171 (-35.96%)
Mutual labels:  geocoding, maps, mapping, google-maps
geocoder
Geocoder is a Typescript library which helps you build geo-aware applications by providing a powerful abstraction layer for geocoding manipulations
Stars: ✭ 28 (-89.51%)
Mutual labels:  autocomplete, geocoding, google-maps, geocode
Googleapi
C# .NET Core Google Api (Maps, Places, Roads, Search, Translate). Supports all endpoints and requests / responses.
Stars: ✭ 346 (+29.59%)
Mutual labels:  autocomplete, geocoding, maps, location
Mobile Sdk
CARTO Mobile SDK core project
Stars: ✭ 116 (-56.55%)
Mutual labels:  geocoding, maps, location
GoogleMapsHelper
An easy to integrate Model Based Google Maps Helper (SVHTTPClient, AFNetworking) That lets you Geo Code , Reverse Geocode, Get Directions , Places Autocomplete.
Stars: ✭ 21 (-92.13%)
Mutual labels:  autocomplete, maps, google-maps
Maps Api For Javascript Examples
Self-contained examples for Maps API for JavaScript v3.
Stars: ✭ 130 (-51.31%)
Mutual labels:  geocoding, maps, mapping
Sketch Map Generator
Sketch plugin to fill a shape with a map generated from a given location using Google Maps and Mapbox
Stars: ✭ 824 (+208.61%)
Mutual labels:  maps, google-maps, mapbox
Placepicker
Free Android Map Place Picker alternative using Geocoder instead of Google APIs
Stars: ✭ 126 (-52.81%)
Mutual labels:  maps, google-maps, location
Gmapsfx
Java API for using Google Maps within a JavaFX application.
Stars: ✭ 233 (-12.73%)
Mutual labels:  maps, mapping, google-maps
country-bounding-boxes
A list of ISO 3166-1 country codes and their bounding boxes.
Stars: ✭ 26 (-90.26%)
Mutual labels:  geocoding, google-maps, mapbox
Examples
Self-contained examples for the legacy Maps API for JavaScript.
Stars: ✭ 78 (-70.79%)
Mutual labels:  geocoding, maps, mapping
Eon Map
Realtime maps with PubNub and MapBox.
Stars: ✭ 121 (-54.68%)
Mutual labels:  maps, google-maps, mapbox
Lenz
Console based MAP 🗺 : with lots of features 🤩
Stars: ✭ 51 (-80.9%)
Mutual labels:  maps, mapping, location
google maps
🗺 An unofficial Google Maps Platform client library for the Rust programming language.
Stars: ✭ 40 (-85.02%)
Mutual labels:  geocoding, maps, google-maps
Use Places Autocomplete
😎 📍 React hook for Google Maps Places Autocomplete.
Stars: ✭ 739 (+176.78%)
Mutual labels:  autocomplete, geocoding, google-maps
EasyWayLocation
This library contain all utils related to google location. like, getting lat or long, Address and Location Setting dialog, many more...
Stars: ✭ 142 (-46.82%)
Mutual labels:  maps, google-maps, location
Leku
🌍 Map location picker component for Android. Based on Google Maps. An alternative to Google Place Picker.
Stars: ✭ 612 (+129.21%)
Mutual labels:  maps, google-maps, location
Maps
🌍🌏🌎 The whole world fits inside your cloud!
Stars: ✭ 253 (-5.24%)
Mutual labels:  maps, location, mapbox
kirby-locator
A simple map & geolocation field, built on top of open-source services and Mapbox. Kirby 3 only.
Stars: ✭ 83 (-68.91%)
Mutual labels:  geocoding, location, mapbox




Svelte MapBox

js-standard-style svelte-v3 publish

Maps and Geocoding (Autocomplete) MapBox 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.

<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
  options={{ scrollZoom: false }} // // add arbitrary options to the map from the mapbox api
>
  <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' }} on:eventname={eventHandler} />
  <ScaleControl />
</Map>

<script>
  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
  }
</script>

<style>
    :global(.mapboxgl-map) {
        height: 200px;
        // sometimes mapbox objects don't render as expected; troubleshoot by changing the height/width to px
    }
</style>

Markers

By default, markers are typical map pins to which you can pass a color property.

<Marker color={brandColour} />

You may also create a custom pin with the default slot.

<Marker
lat={waypoint.geo.lat}
lng={waypoint.geo.lng}
> 
  <a href={waypoint.slug}>
    <div class='myMarker {($mapData.activeMarker == waypoint.id) ? 'active' : ''}' 
    style="
    color:{mainPoint.color};
    background-image: url('{(waypoint.images != undefined) ? waypoint.images[0].thumb.url : ''}');
    ">
    </div>
  </a>
</Marker>

Marker Popups

By default a popup is revealed when you click a pin. It is populated with text provided in the label property.

<Marker label={markerText} />

To indicate interactivity, you may target the marker with some custom CSS:

<style>
    :global(.mapboxgl-marker){
      cursor: pointer;
    }   
</style>

Optionally, disable the popup with the popup={false} property:

<Marker popup={false} />

You may alternatively pass a custom DOM element to the marker to use as a popup.

<Marker lat={pin.coordinates.latitude} lng={pin.coordinates.longitude}>
    <div class="content" slot="popup">
      <h3>{pin.name}</h3>
        <Markdown source={pin.description} />
      </div>
      <img src="{pin.images.length > 0 ? pin.images[0].url : ""}" alt="{pin.name}"/>
    </div> 
</Marker>

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:

<Map accessToken="<your api key>" bind:center bind:zoom>
  <Marker bind:lat bind:lng />
</Map>

<script>
  let center
  let zoom

  $: lng = center[0]
  $: lat = center[1]
</script>

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.

<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.

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].