我正在使用 React Map GL,我想显示单击的国家/地区的名称(我只需要用于其他目的的名称)。 目前,我有这个代码:
import React, { useCallback, useMemo, useState } from "react";
import Map, {
Marker,
NavigationControl,
FullscreenControl,
ScaleControl,
GeolocateControl,
Source,
Layer,
} from "react-map-gl";
import "mapbox-gl/dist/mapbox-gl.css";
import Pin from "../../atoms/Pin/Pin";
import "./Map.scss";
import CITIES from "./cities";
import { useNavigate } from "react-router";
const MapNew = () => {
const navigate = useNavigate();
const [hoveredCountry, setHoveredCountry] = useState(null);
const [cursor, setCursor] = useState<string>("auto");
const onClick = useCallback((event) => {
const feature = event.features && event.features[0];
if (feature) {
const countryName = feature.properties.name;
window.alert(`Clicked on ${countryName}`);
}
}, []);
const pins = useMemo(
() =>
CITIES.map((city, index) => (
<Marker
key={`marker-${index}`}
longitude={city.longitude}
latitude={city.latitude}
anchor="bottom"
>
<Pin />
</Marker>
)),
[]
);
const geojsonUrl =
"https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_50m_admin_0_countries.geojson";
const sourceId = "countries";
const layerId = "country-fills";
return (
<div className="map-parent-wrapper">
<Map
initialViewState={{
latitude: 51.1657,
longitude: 10.4515,
zoom: 3,
bearing: 0,
pitch: 0,
}}
mapStyle="mapbox://styles/mapbox/navigation-day-v1"
mapboxAccessToken=""
scrollZoom={false}
onClick={onClick}
cursor={cursor}
>
<GeolocateControl position="top-left" />
<FullscreenControl position="top-left" />
<NavigationControl position="top-left" />
<ScaleControl />
<Source id={sourceId} type="geojson" data={geojsonUrl}>
<Layer
id={layerId}
type="fill"
source={sourceId}
paint={{
"fill-color": [
"case",
["==", ["get", "name"], "Austria"],
"blue", // Color for Austria
["==", ["get", "name"], "Croatia"],
"green", // Color for Croatia
"gray", // Default color
],
"fill-opacity": 0.5,
}}
/>
</Source>
{pins}
</Map>
</div>
);
};
export default MapNew;
无论我做什么,即使着色逻辑有效,我也无法访问 onClick 名称:
所以,我希望当我点击一个有颜色的国家时,我能得到它的名字。
你可以这样做:
const mapRef = useRef(null);
const onClick = useCallback(async (event) => {
console.log(mapRef);
const map = mapRef.current.getMap();
const features = await map.queryRenderedFeatures(event.point, {
layers: [layerId],
});
if (features.length > 0) {
const clickedCountryName = features[0].properties.name;
console.log(`clicked: ${clickedCountryName}`);
navigate(`/${clickedCountryName}`);
}
}, []);
<Map
initialViewState={{
latitude: 51.1657,
longitude: 10.4515,
zoom: 3,
bearing: 0,
pitch: 0,
}}
mapStyle="mapbox://styles/mapbox/navigation-day-v1"
mapboxAccessToken=""
scrollZoom={false}
onClick={onClick}
cursor={cursor}
ref={mapRef} // add this
>