[Bug] Marker beaving weirdly when i try to drag it with maxBounds defined and the map reached maxBounds
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 35/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 停滞
- 技术栈
- react, typescript
调研方向
使用 react-map-gl 8.0.4 和 mapbox-gl 3.12.0 复现提供的 React 组件,重点关注 maxBounds、可拖动的 Marker,以及 handleDragEnd 中的 setMarkerPosition 更新。跟踪地图到达每个边界时的标记拖动行为;完成的标准是,在边界仍然受到约束的情况下,拖动不再导致标记跳动。
由索引模型根据 Issue 内容生成。
描述
Description
When i try to move my marker, it behaves normally, but when i zoom out so i reach the maxBounds, either on top/left/right/bottom, and then, when i try to move my marker, it jumps randomly across the map
video (showing how my marker behaves normally, but when i reach the edge of the map and try to move it, bug starts to occur):
https://github.com/user-attachments/assets/b3f76ff2-cd44-4fca-adbb-30f94de45b6f
If needed, i can add the component code here
Expected Behavior
Marker should not behave differently when the map is at the edge of the bounds
Steps to Reproduce
Here's my component code, maybe it will be useful:
import { useState, useCallback, useRef, useEffect } from 'react';
import Map, {
NavigationControl,
GeolocateControl,
Marker,
} from 'react-map-gl/mapbox';
import 'mapbox-gl/dist/mapbox-gl.css';
import { MapFill } from '../styles';
import { useData } from '@/context/DataContext';
import {
LngLatBoundsLike,
} from 'mapbox-gl';
import EntityDetailsPanel from '@/components/Mapbox/EntityDetailsPanel';
import GeoJsonDataProcessor from './GeoJsonDataProcessor';
import PieChartImageGenerator from './PieChartImageGenerator';
interface MapComponentProps {
searchMarker?: { longitude: number; latitude: number } | null;
searchingViaInput?: { longitude: number; latitude: number } | null;
onMarkerDrag?: (position: { longitude: number; latitude: number }) => void;
radiusSize: number;
isDragEnabled: boolean;
isRadiusVisible: boolean;
}
export default function MapComponent({
searchMarker,
searchingViaInput,
onMarkerDrag,
radiusSize,
isDragEnabled,
isRadiusVisible,
}: MapComponentProps) {
const [viewState, setViewState] = useState({
longitude: 15.9819,
latitude: 44.915,
zoom: 7,
});
const [markerPosition, setMarkerPosition] = useState(
searchMarker || { longitude: 0, latitude: 0 }
);
const [selectedId, setSelectedId] = useState<string | null>(null);
const [hoveredId, setHoveredId] = useState<string | null>(null);
const [mapLoaded, setMapLoaded] = useState(false);
const [activeEntity, setActiveEntity] = useState<any>(null);
const activeEntityIdRef = useRef<string | null>(null);
const mapRef = useRef<any>(null);
const { entities } = useData();
const { data: geoJson, loading, error } = entities;
const mapBounds: LngLatBoundsLike = [
[-7, 37],
[37.8, 52],
];
// Handle map click
const handleMapClick = useCallback((e: any) => {
if (!mapRef.current) return;
try {
const layers = mapRef.current.getStyle().layers || [];
const layerExists = layers.some(
(l: any) =>
l.id === 'unselected-entities' || l.id === 'selected-entities'
);
if (!layerExists) return;
const features = mapRef.current.queryRenderedFeatures(e.point, {
layers: ['unselected-entities', 'selected-entities'],
});
if (features.length > 0) {
const clickedFeature = features[0];
const id = clickedFeature.properties?.id;
if (activeEntityIdRef.current === id) {
return;
}
setSelectedId(id);
activeEntityIdRef.current = id;
const cleanProperties = {
...clickedFeature.properties,
kategorije: clickedFeature.properties.kategorije
? JSON.parse(clickedFeature.properties.kategorije)
: [],
mainCats: clickedFeature.properties.mainCats
? JSON.parse(clickedFeature.properties.mainCats)
: [],
groupedCategories: clickedFeature.properties.groupedCategories
? JSON.parse(clickedFeature.properties.groupedCategories)
: [],
};
setActiveEntity(cleanProperties);
} else {
setSelectedId(null);
activeEntityIdRef.current = null;
setActiveEntity(null);
}
} catch (error) {
console.warn('Error querying features:', error);
}
}, []);
// Handle mouse move
const handleMouseMove = useCallback((e: any) => {
if (!mapRef.current) return;
try {
const layers = mapRef.current.getStyle().layers || [];
const layerExists = layers.some(
(l: any) =>
l.id === 'unselected-entities' || l.id === 'selected-entities'
);
if (!layerExists) return;
const features = mapRef.current.queryRenderedFeatures(e.point, {
layers: ['unselected-entities', 'selected-entities'],
});
if (features.length > 0) {
setHoveredId(features[0].properties?.id);
mapRef.current.getCanvas().style.cursor = 'pointer';
} else {
setHoveredId(null);
mapRef.current.getCanvas().style.cursor = '';
}
} catch (error) {
console.warn('Error querying features:', error);
}
}, []);
// Handle mouse leave
// const handleMouseLeave = useCallback(() => {
// setHoveredId(null);
// if (mapRef.current) {
// mapRef.current.getCanvas().style.cursor = '';
// }
// }, []);
// Handle marker drag
const handleDragEnd = (event: any) => {
const newPosition = {
longitude: event.lngLat.lng,
latitude: event.lngLat.lat,
};
// IF I DISABLE setMarkerPosition PROBLEM DOESN'T OCCUR, BUT I INTRODUCE NEW PROBLEMS
setMarkerPosition(newPosition);
if (onMarkerDrag) onMarkerDrag(newPosition);
};
// Fly to location when searching via input
useEffect(() => {
if (!searchingViaInput || !mapLoaded || !mapRef.current) return;
mapRef.current.flyTo({
center: [searchingViaInput.longitude, searchingViaInput.latitude],
zoom: 9,
duration: 300,
});
}, [searchingViaInput, mapLoaded]);
// Update marker position on search
useEffect(() => {
if (searchMarker) {
setMarkerPosition(searchMarker);
}
}, [searchMarker]);
const token = process.env.NEXT_PUBLIC_MAPBOX_ACCESS_TOKEN;
if (!token) return <p>No Mapbox token.</p>;
if (loading) return <p>Loading entities…</p>;
if (error) return <p>Error: {String(error)}</p>;
if (!geoJson) return <p>No data.</p>;
return (
<MapFill>
{activeEntity && (
<EntityDetailsPanel
entity={activeEntity}
onClose={() => {
setSelectedId(null);
activeEntityIdRef.current = null;
setActiveEntity(null);
}}
/>
)}
<Map
ref={mapRef}
mapboxAccessToken={token}
mapStyle="mapbox://styles/mapbox/streets-v12"
{...viewState}
onMove={(ev) => setViewState(ev.viewState)}
onClick={handleMapClick}
onMouseMove={handleMouseMove}
dragRotate={false}
minZoom={6.2}
maxZoom={18}
maxBounds={mapBounds} //When bounds are set, on map bounds, marker breaks
// onMouseLeave={handleMouseLeave} //not using
onLoad={() => setMapLoaded(true)}
interactiveLayerIds={['unselected-entities', 'selected-entities']}
>
<NavigationControl position="top-right" />
<GeolocateControl position="top-right" trackUserLocation />
<GeoJsonDataProcessor
geoJson={geoJson}
selectedId={selectedId}
hoveredId={hoveredId}
markerPosition={markerPosition}
radiusSize={radiusSize}
isRadiusVisible={isRadiusVisible}
/>
<PieChartImageGenerator
mapRef={mapRef}
mapLoaded={mapLoaded}
geoJson={geoJson}
/>
{markerPosition && (
<Marker
longitude={markerPosition.longitude}
latitude={markerPosition.latitude}
color="red"
style={{ cursor: isDragEnabled?"pointer":"" }}
draggable={isDragEnabled}
onDragEnd={handleDragEnd}
/>
)}
</Map>
</MapFill>
);
}
Environment
-
Framework version: "react-map-gl": "^8.0.4",
-
Map library: "mapbox-gl": "^3.12.0",
-
Browser: all browsers
-
OS: windows
Logs
No response
- 主要语言
- TypeScript
- 星标
- 8.5k
- 派生
- 1.3k
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
visgl/react-map-gl 的其他 Issue
-
DEFAULT_SETTINGS.maxBounds in @vis.gl/react-maplibre has 360° longitude span, triggers maplibre-gl-js#6148 crash via _updateSettings fallback可能重新可做 关联的 PR 已关闭且未合并。 未关闭
难度 2/5 1-3 小时 新手友好度 74/100
visgl/react-map-gl#2591 · 2 条评论 ·
-
[Bug] Mapbox geocoder example crashes: "i is not a function"可能已有人在做 @yuiseki 于 20 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 35/100
visgl/react-map-gl#2628 ·
-
[Bug] Scroll zoom stops short of maxZoom in controlled mode with MapLibre可能已有人在做 @sviat-barbutsa 于 33 天前认领。 未关闭bug
难度 3/5 半天 新手友好度 74/100
visgl/react-map-gl#2626 ·
-
[Bug] reuseMaps: Mapbox.reuse() infinite-loops (tab freeze) when the recycled map's container is the mount container可能已有人在做 @mikeyfarina 于 82 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 68/100
visgl/react-map-gl#2613 ·
-
feature
难度 5/5 一周以上 新手友好度 42/100
visgl/react-map-gl#2603 · 1 个 reaction ·
查看 visgl/react-map-gl 的全部 Issue
相似的 Issue
-
accessibility area:ci ci-test level:low priority:high type:test
难度 2/5 1-3 小时 新手友好度 71/100
skytuhua-org/Gen-V1#3132 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
drmrd/vernacular#1238 ·
维护者通常 1 天内回复
-
question
难度 2/5 1-3 小时 新手友好度 65/100
-
enhancement migration-prioritized
难度 2/5 1-3 小时 新手友好度 88/100
Hudson-Pufferfish/local-party-console#136 ·
维护者通常 1 天内回复
-
priority:medium
难度 2/5 1-3 小时 新手友好度 75/100
维护者通常 1 天内回复