New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

react-map-gl

Package Overview
Dependencies
Maintainers
6
Versions
292
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-map-gl

React components for MapLibre GL JS and Mapbox GL JS

  • 7.1.8
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
429K
decreased by-14%
Maintainers
6
Weekly downloads
 
Created

What is react-map-gl?

react-map-gl is a React wrapper for Mapbox GL JS, which is a powerful library for interactive, customizable maps. It allows developers to integrate Mapbox maps into their React applications with ease, providing a range of features for map rendering, user interaction, and data visualization.

What are react-map-gl's main functionalities?

Basic Map Rendering

This code demonstrates how to render a basic map using react-map-gl. It sets up a map centered on a specific latitude and longitude with a given zoom level. The map style is set to 'streets-v11' from Mapbox, and the viewport state is managed to allow for user interaction.

```jsx
import React from 'react';
import ReactMapGL from 'react-map-gl';

const Map = () => {
  const [viewport, setViewport] = React.useState({
    latitude: 37.7577,
    longitude: -122.4376,
    zoom: 8
  });

  return (
    <ReactMapGL
      {...viewport}
      width="100%"
      height="100%"
      mapStyle="mapbox://styles/mapbox/streets-v11"
      onViewportChange={nextViewport => setViewport(nextViewport)}
      mapboxApiAccessToken={process.env.REACT_APP_MAPBOX_TOKEN}
    />
  );
};

export default Map;
```

Adding Markers

This code sample shows how to add a marker to the map. The Marker component is used to place a marker at a specific latitude and longitude. In this example, a simple red dot is used as the marker.

```jsx
import React from 'react';
import ReactMapGL, { Marker } from 'react-map-gl';

const MapWithMarkers = () => {
  const [viewport, setViewport] = React.useState({
    latitude: 37.7577,
    longitude: -122.4376,
    zoom: 8
  });

  return (
    <ReactMapGL
      {...viewport}
      width="100%"
      height="100%"
      mapStyle="mapbox://styles/mapbox/streets-v11"
      onViewportChange={nextViewport => setViewport(nextViewport)}
      mapboxApiAccessToken={process.env.REACT_APP_MAPBOX_TOKEN}
    >
      <Marker latitude={37.7577} longitude={-122.4376}>
        <div style={{ backgroundColor: 'red', width: '10px', height: '10px', borderRadius: '50%' }} />
      </Marker>
    </ReactMapGL>
  );
};

export default MapWithMarkers;
```

Handling Map Events

This example demonstrates how to handle map events, such as clicks. The handleClick function is triggered when the map is clicked, displaying an alert with the coordinates of the click.

```jsx
import React from 'react';
import ReactMapGL from 'react-map-gl';

const MapWithEvents = () => {
  const [viewport, setViewport] = React.useState({
    latitude: 37.7577,
    longitude: -122.4376,
    zoom: 8
  });

  const handleClick = (event) => {
    alert(`Clicked at ${event.lngLat}`);
  };

  return (
    <ReactMapGL
      {...viewport}
      width="100%"
      height="100%"
      mapStyle="mapbox://styles/mapbox/streets-v11"
      onViewportChange={nextViewport => setViewport(nextViewport)}
      mapboxApiAccessToken={process.env.REACT_APP_MAPBOX_TOKEN}
      onClick={handleClick}
    />
  );
};

export default MapWithEvents;
```

Other packages similar to react-map-gl

Keywords

FAQs

Package last updated on 19 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc