1.0.43 • Published 5 years ago

the-map v1.0.43

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

the-map

Build Status npm Version JS Standard

Geo map for the-components

Installation

$ npm install the-map --save

Usage

'use strict'

import React from 'react'
import { TheMap, TheMapStyle, TheMapPositionInput } from 'the-map'
import { TheSpinStyle } from 'the-spin'

// @see https://leaflet-extras.github.io/leaflet-providers/preview/
const MapLayers = [
  {
    key: 'layer01',
    title: 'Layer 01',
    url: 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
    maxZoom: 19,
    attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
  },
  {
    key: 'layer02',
    title: 'Layer 02',
    url: 'http://{s}.tiles.wmflabs.org/bw-mapnik/{z}/{x}/{y}.png',
    maxZoom: 18,
    attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
  },
]

class ExampleComponent extends React.Component {
  handleLeafletMap = (map) => {
    this.map = map
  }
  handleChange = ({ lat, lng, zoom, bounds: { west, south, east, north } }) => {
    this.setState({ lat, lng, zoom })
    console.log('bounds changed', { west, south, east, north })
  }
  handleUpdate = ({ pos01 }) => {
    const { lat, lng, zoom } = pos01
    this.setState({
      post01: { lat, lng, zoom },
    })
  }

  moveToCurrent = () => {
    navigator.geolocation.getCurrentPosition(({ coords }) => {
      const { latitude: lat, longitude: lng } = coords
      this.setState({ lat, lng })
    }, () => alert('Failed to get current position'))
  }

  setMarker(key, values) {
    const { markers } = this.state
    this.setState({
      markers: [
        ...markers.filter((m) => m.key !== key),
        { key, ...values }
      ]
    })
  }

  getMarker(key) {
    return this.state.markers.find((m) => m.key === key)
  }

  handleClick = ({ lat, lng }) => {
    const { markers } = this.state
    console.log('Map clicked', { lat, lng })
    let name = `m${String(markers.length + 1).padStart(2, '0')}`
    let markerValues = {
      lat,
      lng,
      onClick: () => console.log('node clicked', name),
      node: (
        <div style={{
          borderRadius: '50%',
          textAlign: 'center',
          background: '#EE1',
          width: 22,
          height: 22,
          color: 'white',
          lineHeight: '22px',
        }}>
          <div>{name}</div>
        </div>
      ),
    }
    this.setMarker(name, markerValues)

    setInterval(() => {
      const marker = this.getMarker(name)
      const lng = marker.lng + 0.001
      const lat = marker.lat + 0.001
      this.setMarker(name, {
        ...marker,
        lat,
        lng,
      })
      console.log('Marker moved', name, { lat, lng })
    }, 1000)
  }

  state = {
    lat: 35.6895,
    lng: 139.6917,
    zoom: 13,
    markers: [
      {
        key: 'marker-01',
        lat: 51.505,
        lng: -0.09,
        onClick: () => console.log('marker01 clicked'),
        node: (
          <div style={{
            borderRadius: '50%',
            textAlign: 'center',
            background: '#E33',
            width: 48,
            height: 48,
            color: 'white',
            lineHeight: '48px',
          }}>
            <div>Mrkr01</div>
          </div>
        ),
      }
    ],
    popups: [
      {
        key: 'popup-01',
        for: 'marker-01',
        node: (
          <div>This is popup01 for marker01</div>
        )
      }
    ]
  }

  render() {
    const { state: { lat, lng, zoom, markers, post01 } } = this
    return (
      <div>
        <TheSpinStyle />
        <TheMapStyle />
        <TheMap onLeafletMap={this.handleLeafletMap}
                onChange={this.handleChange}
                {...{ lat, lng, zoom }}
                width={'480px'}
                height={'50vh'}
                layers={MapLayers}
                onClick={this.handleClick}
                markers={markers}
        >
        </TheMap>

        <hr />

        <button
          onClick={this.moveToCurrent}>Move to current
        </button>


        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <hr />
        <section>
          <h1>As Input</h1>
          <TheMapPositionInput value={post01}
                               name={'pos01'}
                               onUpdate={this.handleUpdate}
          />
        </section>
      </div>

    )
  }
}

export default ExampleComponent

Components

TheMap

Geo map for the-components

Props

NameTypeDescriptionDefault
freezedboolDisable all interactionsfalse
heightunionHeight of mapnull
latnumberlatitude value
lngnumberlongitude value
onLeafletMapfuncCallback when map map creatednull
spinningboolShows spinnerfalse
widthunionWidth of mapnull
layerControlEnabledtrue
layerControlPosition'topright'
layers`[

{ attribution: '© OpenStreetMap', key: 'default', maxZoom: 19, url: 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', }, ]| |markers| | |[]| |zoomControlEnabled| | |true| |zoomControlPosition| | |'topleft'` |

TheMapMarker

TheMapPositionInput

Props

NameTypeDescriptionDefault
idstring
namestring
valueunion{ lat: 35.6895, lng: 139.6917, zoom: 13 }
height150
layersTheMap.defaultProps.layers || []
width300

License

This software is released under the MIT License.

Links

1.0.43

5 years ago

1.0.42

5 years ago

1.0.41

5 years ago

1.0.40

5 years ago

1.0.39

5 years ago

1.0.38

5 years ago

1.0.37

5 years ago

1.0.36

5 years ago

1.0.35

5 years ago

1.0.34

5 years ago

1.0.33

5 years ago

1.0.32

5 years ago

1.0.31

5 years ago

1.0.30

5 years ago

1.0.29

5 years ago

1.0.28

5 years ago

1.0.27

5 years ago

1.0.26

5 years ago

1.0.25

5 years ago

1.0.24

5 years ago

1.0.23

5 years ago

1.0.22

5 years ago

1.0.21

5 years ago

1.0.20

5 years ago

1.0.19

5 years ago

1.0.18

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago