1.0.4 • Published 5 years ago

react-map-gl-directions v1.0.4

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

react-map-gl-directions is a React wrapper for Mapbox GL Directions for use with react-map-gl.

Table of Contents

Installation

Using react-map-gl-directions requires react >= 16.3.

npm install --save react-map-gl-directions

Styling

Import:

import 'react-map-gl-directions/dist/mapbox-gl-directions.css'

or

Link tag in header:

<link
  href="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-directions/v4.0.0/mapbox-gl-directions.css"
  rel="stylesheet"
/>

Features

Only mapRef and mapboxApiAccessToken are required.

NameTypeDefaultDescription
mapRefObjectRef for react-map-gl map component.
mapboxApiAccessTokenStringhttps://www.mapbox.com/
positionString"top-right"Position on the map to which the directions plugin control will be added. Valid values are "top-left", "top-right", "bottom-left", and "bottom-right".
apiStringOverride default ("https://api.mapbox.com/directions/v5/") routing endpoint url.
interactiveBooleantrueEnable/Disable mouse or touch interactivity from the plugin.
profileString"mapbox/driving-traffic"Routing profile to use. Options: mapbox/driving-traffic, mapbox/driving, mapbox/walking, mapbox/cycling.
stylesArrayOverride default layer properties of the directions source. Documentation for each property are specified in the Mapbox GL Style Reference
alternativesBooleanfalseWhether to enable alternatives.
congestionBooleanfalseWhether to enable congestion along the route line.
unitString"imperial"Measurement system to be used in navigation instructions. Options: imperial, metric.
compileFunctionnullProvide a custom function for generating instruction, compatible with osrm-text-instructions.
geocoderObjectAccepts an object containing the query parametres as documented here.
controlsObjectHide or display inputs, instructions and profile switch controls.
zoomNumber16If no bbox exists from the geocoder result, the zoom you set here will be used in the flyTo.
placeholderOriginString"Choose a starting place"If set, this text will appear as the placeholder attribute for the origin input element.
placeholderDestinationString"Choose destination"If set, this text will appear as the placeholder attribute for the destination input element.
flyToBooleantrueIf false, animating the map to a selected result is disabled.
excludeStringnullExclude certain road types from routing. The default is to not exclude anything. Options: ferry, toll, motorway.
onInitFunction() => {}Is passed Mapbox directions instance as param and is executed after Mapbox directions plugin is initialized.
onClearFunction() => {}Is passed { type: } as a param and type is one of 'origin' or 'destination'. Fired when input is cleared.
onLoadingFunction() => {}Is passed { type: } as a param and type is one of 'origin' or 'destination'. Fired when either origin or destination is loading.
onOriginFunction() => {}Is passed { feature } Fired when origin is set.
onDestinationFunction() => {}Is passed { feature } Fired when destination is set.
onProfileFunction() => {}Is passed { profile } Profile is one of 'driving', 'walking', or 'cycling'. Fired when profile is set.
onRouteFunction() => {}Is passed { route } Fired when a route is updated
onErrorFunction() => {}Is passed { error } as a param. Error as string.

Example

import 'mapbox-gl/dist/mapbox-gl.css'
import 'react-map-gl-directions/dist/mapbox-gl-directions.css'
import React, { Component } from 'react'
import MapGL from 'react-map-gl'
import Directions from 'react-map-gl-directions'

function getAccessToken() {
  var accessToken = null

  if (typeof window !== 'undefined' && window.location) {
    var match = window.location.search.match(/access_token=([^&\/]*)/)
    accessToken = match && match[1]
  }

  if (!accessToken && typeof process !== 'undefined') {
    // Note: This depends on bundler plugins (e.g. webpack) inmporting environment correctly
    accessToken = accessToken || process.env.MapboxAccessToken // eslint-disable-line
  }

  return accessToken || null
}

// Ways to set Mapbox token: https://uber.github.io/react-map-gl/#/Documentation/getting-started/about-mapbox-tokens
const MAPBOX_TOKEN = getAccessToken()

class Example extends Component {
  state = {
    viewport: {
      latitude: 37.7577,
      longitude: -122.4376,
      zoom: 8,
    },
  }

  mapRef = React.createRef()

  handleViewportChange = (viewport) => {
    this.setState({
      viewport: { ...this.state.viewport, ...viewport },
    })
  }

  render() {
    return (
      <MapGL
        ref={this.mapRef}
        {...this.state.viewport}
        width="100%"
        height="100%"
        onViewportChange={this.handleViewportChange}
        mapboxApiAccessToken={MAPBOX_TOKEN}>
        <Directions mapRef={this.mapRef} mapboxApiAccessToken={MAPBOX_TOKEN} />
      </MapGL>
    )
  }
}

export default Example

react-map-gl-directions example screenshot

Support

Reach out to me at one of the following places!

  • Linkedin at Neelesh Bisht
  • Mail at neeleshbisht99@gmail.com
  • Twitter at @neeleshbisht99

License

License