0.1.4 • Published 5 years ago

leaflet-spots v0.1.4

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

Introduce

leaflet-spots is a paradigm and tool for rendering spots on a leaflet map.


Motivation

Part of my job is data visualization based on geographic location. For example:

  • Texi/bus realtime positioning
  • IoT realtime positioning
  • Satellites projecting on earth
  • User distribution

Taxis, buses, IoT devices, satellite projections, users need to be drawn on the map, and I call them spot. A taxi is a spot, a user is a spot, etc.

See this example:

This kind of projects have an almost same logic, so I decided to abstract it out to be a common extension of leaflet.


Demo

https://codesandbox.io/s/leaflet-spot-demo-80eyw


Installation

# install dependency
yarn add leaflet
// or
// npm install --save leaflet
# install leaflet-spots
yarn add leaflet-spots
// or
// npm install --save leaflet-spots

Quick Start

See this demo with source code:

https://codesandbox.io/s/leaflet-spot-demo-80eyw


API Reference

import { LeafletSpots, MetadataParser } from 'leaflet-spots';

class MetadataParser\<T>

Describe how to parse your data in class MetadataParser

T is the type of your metadata like { id: 1, lng: 120, lat: 30 }

MethodParametersReturnDescription
constructorMetadataParserOptions\<T>--
MetadataParserOptions
export interface MetadataParserOptions<T> {
  /**
   * parse lat,lng from metadata
   */
  parseLatlng: LatlngParser<T>;
  /**
   * parse leaflet shape from metadata
   */
  parseShape: ShapeParser<T>;
  /**
   * parse id from metadata
   */
  parseId: IdParser<T>;
  /**
   * parse whether the station should be update
   */
  parseShouldUpdate?: ShouldUpdateParser<T>;
  /**
   * parse whether the station is filtered
   */
  parseFilteration?: FilterationParser<T>;
}
Parser types
export type LatlngParser<T> = (metadata: T) => LatLng;
export type ShapeParser<T> = (metadata: T) => Layer;
export type IdParser<T> = (metadata: T) => string;
export type ShouldUpdateParser<T> = (prev: T, next: T) => boolean;
export type FilterationParser<T> = (metadata: T) => boolean;

class LeafletSpots\<T>

Using this class to visualize your data

T is the type of your metadata like { id: 1, lng: 120, lat: 30 }

MethodParametersReturnDescription
constructorLeafletSpotsOptions\<T>--
getLayer-LayerGroupThe layer group that holds all the spots
setSpotsT[]voidSet spots data, LeafletSpots will render them to leaflet map instance
addSpotTvoidAdd a spot to leaflet map instance
removeSpotTvoidRemove a spot from leaflet map instance
updateSpotTvoidUpdate a spot in leaflet map instance
selectSpotTvoidMark this spot to be selected
unselectSpotTvoidCancel selection
forceRender-voidForce rerender all spots

LeafletSpotsOptions
/**
 * The options to create the instance of `LeafletSpots`
 * @template T User data unit
 */
export interface LeafletSpotsOptions<T> {
  /**
   * Use `MetadatParser` to parse user data.
   * @template T User data unit
   */
  metadataParser: MetadataParser<T>;
  /**
   * The spot events which will be attached to the spot.
   * @template T User data unit
   */
  spotEvents?: SpotEvents<T>;
  /**
   * handle interactive like 'selected', 'filtered', etc
   * @template T User data unit
   */
  handleInteractive?: InteractiveHandler<T>;
}
SpotEvents\<T>
/**
 * The spot events which will be attached to the spot.
 * @template T User data unit
 */
export interface SpotEvents<T> {
  [eventName: string]: (e: LeafletEvent, metadata: T) => void;
}
InteractiveHandler\<T>
/**
 * Interactive options.
 */
export interface InteractiveOptions {
  filtered: boolean;
  selected: boolean;
}

/**
 * User defined handler for interactive.
 * @template T User data unit
 */
export type InteractiveHandler<T> = (
  metadata: T,
  shape: Layer,
  options: InteractiveOptions,
) => void;

Road Map

  • Support Typescript
  • Support Javascript
  • Testing
  • Logo
  • User manual
    • Introduce
    • Quick Start
    • API Reference
  • Demo page
  • Publish to NPM
0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago