メインコンテンツまでスキップ

Search Box - React Quickstart

Search Box is a Mapbox Search JS feature that allows users to quickly search for places, addresses, and points of interest. It is powered by the Mapbox Search Box API and can be used in a web or Node environment.

Search Box UI components can be dropped into websites as standalone elements or as part of a comprehensive map experience.

This Quickstart Guide will help you get started with Search using the Mapbox Search JS React framework.

To use Search Box in a website (without React), see the Search Box Web Quickstart.

Prerequisites

To use Mapbox Search JS, you need to have a Mapbox access token.

Your default public token

You can use your default public token to get started with the code snippets in this guide.

To get your default public token, login to account.mapbox.com and scroll down to the Access Tokens section.

This access token associates your search requests with a Mapbox account for billing. For more information on creating and using access tokens, see our token management documentation.

Installation

Install the NPM package.

npm install --save @mapbox/search-js-react

Once installed, the SearchBox component will be available for import into your React app.

Use the SearchBox component

Add the SearchBox component wherever you need an interactive search input. Be sure to pass in your Mapbox access token as the prop accessToken, and any other configuration you may need. For the full list of props, see SearchBoxProps in the React reference.

import { SearchBox } from '@mapbox/search-js-react';

const MyComponent = () => {
return (
<div>
<SearchBox
accessToken='YOUR_MAPBOX_ACCESS_TOKEN'
options={{
language: 'en',
country: 'US'
}}
/>
</div>
)
}

export default MyComponent

Integration with a Mapbox GL JS Map

The <SearchBox> component can be used in a standalone fashion, but a common use case is to zoom to a location on a map and show a marker pin after the user chooses a result. <SearchBox> includes convenient props for binding with a Mapbox GL JS Map instance to achieve this functionality.

The map prop binds to the Map instance, and the props mapboxgl and marker are used to create the Marker.

import { useRef, useEffect, useState } from "react";
import { SearchBox } from "@mapbox/search-js-react";
import mapboxgl from "mapbox-gl";
import "mapbox-gl/dist/mapbox-gl.css";

const accessToken = "YOUR_MAPBOX_ACCESS_TOKEN";

export default function MapWithGeocoder() {
const mapContainerRef = useRef();
const mapInstanceRef = useRef();
const [mapLoaded, setMapLoaded] = useState(false);
const [inputValue, setInputValue] = useState("");
useEffect(() => {
mapboxgl.accessToken = accessToken;

mapInstanceRef.current = new mapboxgl.Map({
container: mapContainerRef.current, // container ID
center: [-74.5, 40], // starting position [lng, lat]
zoom: 9, // starting zoom
});

mapInstanceRef.current.on("load", () => {
setMapLoaded(true);
});
}, []);

return (
<>
<SearchBox
accessToken={accessToken}
map={mapInstanceRef.current}
mapboxgl={mapboxgl}
value={inputValue}
onChange={(d) => {
setInputValue(d);
}}
marker
/>
<div id="map-container" ref={mapContainerRef} style={{ height: 300 }} />
</>
);
}



Additional Resources

Quickstart Guides are also available for the other main features of Mapbox Search JS:

Search Box can also be implemented with a custom UI by using the Mapbox Search JS Core Framework.

このpageは役に立ちましたか?